Reputation: 46503
I would like to use some RewriteRule
s to transform :
http://example.com/blah => index.php?id=blah
http://example.com/someone/blah => index.php?id=blah&user=someone
Here is the htaccess
:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1 [L]
RewriteRule ^(.*)/(.*)$ /index.php?id=$2&user=$1 [L]
</IfModule>
But it seems that the two last rules are incompatible : with this htaccess
, I get some 500 Internal Server Error
.
How to create two rules such that they do not "overlap" each other?
Notes :
each of these 2 rules work alone
when I use the second rule, then http://example.com/someone/blah => index.php?id=blah&user=someone
works, but it seems that the root folder is no more /
but /someone/
and then the CSS are not found... How to prevent the base folder to be changed in this case ?
Upvotes: 0
Views: 92
Reputation: 19528
You have 4 issues:
(.*)
will convert anything into /index.php?id=$1
500 internal server error
.domain.com/anything/anything
To fix the redirect issue, you can use this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /index.php?id=$2&user=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?id=$1 [L]
</IfModule>
Since I've changed the regex (to ([^/]+)
which means anything not a /
) that catches the data you want and the order won't matter in this scenario as it will specifically match:
domain.com/anything
And
domain.com/anything/anything
To fix the CSS and Images you can use the base
TAG to define your absolute URL into your HTML:
<base href="http://domain.com/">
Upvotes: 2