aborted
aborted

Reputation: 4541

URL rewriting with Apache .htaccess with two matches

Sorry about this question - I know this is asked a lot, but this case is a little more distinct (for me at least).

I have the following content in my current .htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^/]+)/?$ index.php?mode=$1 [QSA,L]

This rewrites all URLs that are from index.php and have the mode parameter to a simply URL where the mode's content becomes the main part of the URL, like this:

http://someurl.com/index.php?mode=mymode

Becomes:

http://someurl.com/mymode

This is exactly what I need. But I also need to extend this and be able to achieve the same effect with another file of mine named user.php. The case is almost the same:

http://someurl.com/user.php?action=hello

Becomes: 

http://someurl.com/hello

I have no idea how to achieve the second part without conflincting with the first one.

I'm kinda stuck on this one.

Upvotes: 1

Views: 109

Answers (1)

anubhava
anubhava

Reputation: 786091

Keep your rules like this:

# new rules for /user.php?action=hello
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^user/([^/]+)/?$ user.php?action=$1 [QSA,L]

# existing rule for /index.php?mode=mymode
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^/]+)/?$ index.php?mode=$1 [QSA,L]

PS: Also better to use absolute path in your css, js, images files rather than arelative one. Which means you have to make sure path of these files start either with http:// or a slash /.

Upvotes: 1

Related Questions