如何“重写规则” 对于子子目录? .htaccess/php

发布于 2024-07-24 07:21:57 字数 1011 浏览 6 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2024-07-31 07:21:57

R 标志 强制外部重定向。 因此,只需将其保留,请求将在内部重定向:

RewriteRule ^test/([0-9]+)/([A-Za-z0-9-]+)?$ test/test.php?id=$1&key=$2

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文