.htaccess,将所有 PHP 变量重写到文件夹

发布于 2025-01-07 07:28:18 字数 896 浏览 3 评论 0原文

我遇到这个问题了。我想将所有 php 变量写入文件夹。例如:

www.mypage.com/en/shows/1/

www.mypage.com/index.php?language=en&section=shows&item=1 >

www.mypage.com/en/home/

www.mypage.com/index.php?language=en&section=home

这是我得到的:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)/(.*)/$ index.php?language=$1&section=$2
RewriteRule (.*)/(.*)/(.*)/$ index.php?language=$1&section=$2&item=$3

它完美地与www.mypage.com/en/home/

www.mypage.com/index.php?language=en&section=home

但是当我尝试使用 www.mypage.com/en/shows/1/

www.mypage.com/index.php?language=en&section=shows&item=1

var 语言消失。我阅读了这里已经发布的一些答案并找到了一些教程,但说实话我不太了解。

感谢您的耐心等待,我是一个完全的新手。 非常感谢。

I got this problem. I want to write ALL my php vars to folders. For example:

www.mypage.com/en/shows/1/ to

www.mypage.com/index.php?language=en§ion=shows&item=1

or

www.mypage.com/en/home/ to

www.mypage.com/index.php?language=en§ion=home

This is what I got:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)/(.*)/$ index.php?language=$1§ion=$2
RewriteRule (.*)/(.*)/(.*)/$ index.php?language=$1§ion=$2&item=$3

It works perfectly with www.mypage.com/en/home/ to

www.mypage.com/index.php?language=en§ion=home

but when I try with www.mypage.com/en/shows/1/ to

www.mypage.com/index.php?language=en§ion=shows&item=1

the var laguage disapears. I read some of the answers already posted here and find a couple of tutorials but to be honest I don't understand too much.

Thanks for your patience, I am a complete newbie.
Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

愛上了 2025-01-14 07:28:18

我认为主要问题是您的第一个 RewriteRule 将始终匹配,而不是第二个。匹配 (.*)/(.*)/(.*)/$ 的内容也将始终匹配 (.*)/(.*)/$ ,因为后者并不关心它之前的内容。

理想情况下,您希望在开头放置一个 ^ 来阻止这种情况,但如果您不能或它不起作用,您可以随时尝试反转规则,以便具有三个的区域有机会首先匹配。尽管重写应该阻止第二条规则匹配,但最好在它们后面添加 [L] ,除非有其他原因不能这样做。 [L] 表示如果匹配,则不会尝试匹配任何其他规则。

I think the main issue is that your first RewriteRule will always match instead of your second one. Something that matches (.*)/(.*)/(.*)/$ will always match (.*)/(.*)/$ also, because the latter doesn't care what comes before it.

Ideally, you would want to put a ^ at the beginning to stop this, but if you can't or it doesn't work, you could always try just reversing the rules so that the one with three areas gets a chance to match first. And although the rewrite should prevent the second rule from matching, it would be good practice to add [L] after them both, unless there's another reason you can't. [L] means that if it matches, it won't try matching any other rules.

粉红×色少女 2025-01-14 07:28:18

试试这个,

Options +FollowSymLinks
RewriteEngine on
RewriteBase / 

RewriteRule ^([a-zA-Z]{2})/(\w+)/$ index.php?language=$1§ion=$2 [L]
RewriteRule ^([a-zA-Z]{2})/(\w+)/(\d+)/$ index.php?language=$1§ion=$2&item=$3 [L]

我假设有 2 个字符的语言代码

Try this instead

Options +FollowSymLinks
RewriteEngine on
RewriteBase / 

RewriteRule ^([a-zA-Z]{2})/(\w+)/$ index.php?language=$1§ion=$2 [L]
RewriteRule ^([a-zA-Z]{2})/(\w+)/(\d+)/$ index.php?language=$1§ion=$2&item=$3 [L]

I am assuming a 2 character language code

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