对 URL 变量使用 MOD_REWRITE 并在不使用时删除空参数
我正在尝试创建一个 mod_rewrite ,当用户输入时,或者它在 URL 中显示:
www.example.com/var1/var2/var3/var4/
它输出相同,
www.example.com?page=var1&cat=var2&subcat=var3&subsubcat=var4
但如果数字变量发生变化,它仍然会工作,所以变量可能是:
www.example.com/var1/
or
www.example.com/var1/var2/
or
www.example.com/var1/var2/var3/
or
www.example.com/var1/var2/var3/var4/
如果我使用以下内容,它仅适用于 www.example.com/var1/var2/var3/var4/,但不适用于其他内容:
Options +FollowSymLinks RewriteEngine On RewriteBase /
RewriteRule ^([^&]+)/([^&]*)/([^&]*)/([^&]*)/$ /?page=$1&cat=$2&subcat=$3&subsubcat=$4 [L]
如果我使用以下使用 CHAINING 的代码,它在某种程度上可以作为链使用,但问题是对于
www.example.com/var1/var2/
www.example.com/var1/var2/var3/
www.example.com/var1/var2/var3/var4/
它将 VAR1 输出为“/”,因此它使用以下代码忽略第一个变量:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^&]+)/$ /?page=$1 [C]
RewriteRule ^([^&]+)/([^&]*)/$ /?page=$1&cat=$2 [C]
RewriteRule ^([^&]+)/([^&]*)/([^&]*)/$ /?page=$1&cat=$2&subcat=$3 [C]
RewriteRule ^([^&]+)/([^&]*)/([^&]*)/([^&]*)/$ /?page=$1&cat=$2&subcat=$3&subsubcat=$4 [L]
顺序始终相同,因此这应该很容易。请问我该如何进行这项工作?
I am trying to create a mod_rewrite that when a user enters, or it shows in the URL:
www.example.com/var1/var2/var3/var4/
it outputs the same as
www.example.com?page=var1&cat=var2&subcat=var3&subsubcat=var4
but if there is a change in the number variables, it will still work, so the variables could be:
www.example.com/var1/
or
www.example.com/var1/var2/
or
www.example.com/var1/var2/var3/
or
www.example.com/var1/var2/var3/var4/
If I use the following, it only works for www.example.com/var1/var2/var3/var4/ but not the others:
Options +FollowSymLinks RewriteEngine On RewriteBase /
RewriteRule ^([^&]+)/([^&]*)/([^&]*)/([^&]*)/$ /?page=$1&cat=$2&subcat=$3&subsubcat=$4 [L]
If I use the following code using CHAINING, it works as a chain to an extent, but the problem is that for
www.example.com/var1/var2/
www.example.com/var1/var2/var3/
www.example.com/var1/var2/var3/var4/
it outputs the VAR1 as "/", so it is ignoring the first variable using the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^&]+)/$ /?page=$1 [C]
RewriteRule ^([^&]+)/([^&]*)/$ /?page=$1&cat=$2 [C]
RewriteRule ^([^&]+)/([^&]*)/([^&]*)/$ /?page=$1&cat=$2&subcat=$3 [C]
RewriteRule ^([^&]+)/([^&]*)/([^&]*)/([^&]*)/$ /?page=$1&cat=$2&subcat=$3&subsubcat=$4 [L]
The order is always the same so this should be easy. How can I make this work please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能我遗漏了一些东西,但在我看来,你不需要在这里链接
C
标志。请考虑这些规则:我在所有规则中添加了
RewriteCond %{QUERY_STRING} !^page= [NC]
以确保存在无限循环。此外,我还通过在每个规则的末尾添加?
使尾部斜杠成为可选。上述所有规则在我的测试中都运行良好。
Probably I am missing something but IMO you don't need chaining
C
flag here. Please consider these rules:I added
RewriteCond %{QUERY_STRING} !^page= [NC]
in all rules to make sure there is infinite looping. Also I made trailing slash optional by adding?
in the end of each rule.All of above rules are working fine in my testing.