问题
I have one folder named test located at www.mydomain.com/abc/files/test. I need to change the URLs from www.mydomain.com/abc/files/test/test.php?id=15&key=some-text123
to www.mydomain.com/abc/files/test/15/some-text123
this is similiar to SO urls.
I tried following in .htaccess file with following code
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^test/([0-9]+)/([A-Za-z0-9-]+)?$ test/test.php?id=$1&key=$2 [R]
It redirects me to www.mydomain.com/test/test.php?id=15&key=some-text-123
but it didnt work as I copied the format from somewhere. I am not sure ^ should include www part as well or it just assumes / as root?
The "id" part is important to me. I'll also consider followings as valid urls.
www.mydomain.com/abc/files/test/15 (without /some-text123")
www.mydomain.com/abc/files/test/15/ (without /some-text123 but having / )
Can you please help me write correct rule?
How can I make it so that I dont have to hard-code "abc/files/" ?
It shouldn't affect any of the other urls of my site(than "test" folder.)
Also the URL should stay the same (/files/test/15 etc
) instead of changing to "?id=15&key=some-text123"
Thanks a lot.
回答1:
The R flag forces an external redirect. So just leave it away and the request will be redirected internally:
RewriteRule ^test/([0-9]+)/([A-Za-z0-9-]+)?$ test/test.php?id=$1&key=$2
来源:https://stackoverflow.com/questions/1018383/how-to-rewrite-rule-for-a-sub-sub-directory-htaccess-php