如何“重写规则” 对于子子目录? .htaccess/php
我有一个名为 test 的文件夹,位于 www.mydomain.com/abc/files/test。 我需要将网址从 www.mydomain.com/abc/files/test/test.php?id=15&key=some-text123
更改
为 www.mydomain.com/abc /files/test/15/some-text123
这与 SO url 类似。
我尝试在 .htaccess 文件中使用以下代码,
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 部分,或者它只是假设 / 作为根?
“id”部分对我来说很重要。 我还会将以下内容视为有效网址。
www.mydomain.com/abc/files/test/15 (without /some-text123")
www.mydomain.com/abc/files/test/15/ (without /some-text123 but having / )
你能帮我写出正确的规则吗?
我怎样才能做到这样我就不必硬编码“abc/files/”?
它不应该影响我网站的任何其他网址(除了“test”文件夹)。
此外,网址应该保持不变(/files/test/15 等
),而不是更改为 "?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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
R
标志 强制外部重定向。 因此,只需将其保留,请求将在内部重定向:The
R
flag forces an external redirect. So just leave it away and the request will be redirected internally: