OmniPotens
OmniPotens

Reputation: 1125

Conflicting URL Rewriting

I have the below in my htaccess file

RewriteRule ^([A-Za-z0-9-]+)$ /j/view.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)$ /j/display.php?name=$1 [L]

Both rewrite to get URL as http://domain.com/some-file-name. With the way it is, the browser interprets whchever rewrite rule comes first in the line when a link is clicked at since URLs are of same format.

How do I fix it to understand what URL to interpret and display correctly still maintaining the same URL structure.

Currently, if I move the #2 above, it starts interpreting #2 and if I allow as it is, it interprets #1. Please I need some help here.

Upvotes: 1

Views: 220

Answers (2)

anubhava
anubhava

Reputation: 785146

In one of your comments you asked:

If I were to make URL become http://domain.com/some-file-name for "view.php" and http://domain.com/some-file-name---[id] for "display.php" where "[id]" will be the "group_id" number in the database, will it make a difference in their URL and cause it to rewrite well?

Yes this will be possible with following rules:

# first try to see if belongs to /j/view.php based on URI pattern
RewriteRule ^([a-z0-9-]+)---([0-9]+)/?$ /j/view.php?cat=$1&id=$2 [L,NC,QSA]

# No try /j/display.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9-]+)/?$ /j/display.php?name=$1 [L,NC,QSA]

Upvotes: 1

Sumurai8
Sumurai8

Reputation: 20737

If there is no constant difference between the url's, you cannot constantly split the requests between those two pages. I recommend changing the url's so that each of the two pages have their own prefix.

RewriteRule ^c-([A-Za-z0-9-]+)$ /j/view.php?cat=$1 [L]
RewriteRule ^n-([A-Za-z0-9-]+)$ /j/display.php?name=$1 [L]

If you don't wish to do that, you'll have to make a router page that intelligently splits the requests between your two pages. You would have a rule:

RewriteRule ^([A-Za-z0-9-]+)$ /j/router.php?page=$1 [L]

And a file router.php with something like the following code. Obviously I can't tell what the difference is between the two. Maybe you can match it against a database or something.

<?php
if( condition to check $_GET['page'] for view.php ) {
  $_GET['cat'] = $_GET['page'];
  include( '/j/view.php' );
} else {
  $_GET['name'] = $_GET['page'];
  include( '/j/display.php' );
}

Upvotes: 0

Related Questions