如何“重写规则";子目录? .htaccess/php [英] how to "rewrite rule" for a sub-sub-directory? .htaccess/php
问题描述
我在www.mydomain.com/abc/files/test上有一个名为test的文件夹.我需要从www.mydomain.com/abc/files/test/test.php?id=15&key=some-text123
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
到www.mydomain.com/abc/files/test/15/some-text123
这类似于SO网址.
我尝试在.htaccess文件中使用以下代码进行跟踪
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]
它将我重定向到www.mydomain.com/test/test.php?id=15&key=some-text-123
但是它没有用,因为我从某处复制了格式.我不确定^是否也应包括www部分,或者仅假设/为根?
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?
"id"部分对我很重要.我还将考虑以下内容为有效的网址.
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?
如何做到这一点,而不必硬编码"abc/files/"?
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.)
此外,URL应该保持不变(/files/test/15 etc
),而不是更改为"?id=15&key=some-text123"
Also the URL should stay the same (/files/test/15 etc
) instead of changing to "?id=15&key=some-text123"
非常感谢.
推荐答案
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
这篇关于如何“重写规则";子目录? .htaccess/php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!