how to “rewrite rule” for a sub-sub-directory? .htaccess/php

☆樱花仙子☆ 提交于 2019-12-13 07:00:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!