对 URL 变量使用 MOD_REWRITE 并在不使用时删除空参数

发布于 2024-11-03 07:05:14 字数 1341 浏览 4 评论 0原文

我正在尝试创建一个 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 技术交流群。

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

发布评论

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

评论(1

ペ泪落弦音 2024-11-10 07:05:14

可能我遗漏了一些东西,但在我看来,你不需要在这里链接 C 标志。请考虑这些规则:

RewriteEngine on
Options +FollowSymlinks -MultiViews

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/?$ /?page=$1 [L]

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/([^/]*)/?$ /?page=$1&cat=$2 [L]

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/([^/]*)/([^/]*)/?$ /?page=$1&cat=$2&subcat=$3 [L]

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/([^/]*)/([^/]*)/([^/]*)/?$ /?page=$1&cat=$2&subcat=$3&subsubcat=$4 [L]

我在所有规则中添加了 RewriteCond %{QUERY_STRING} !^page= [NC] 以确保存在无限循环。此外,我还通过在每个规则的末尾添加 ? 使尾部斜杠成为可选。

上述所有规则在我的测试中都运行良好。

Probably I am missing something but IMO you don't need chaining C flag here. Please consider these rules:

RewriteEngine on
Options +FollowSymlinks -MultiViews

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/?$ /?page=$1 [L]

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/([^/]*)/?$ /?page=$1&cat=$2 [L]

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/([^/]*)/([^/]*)/?$ /?page=$1&cat=$2&subcat=$3 [L]

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/([^/]*)/([^/]*)/([^/]*)/?$ /?page=$1&cat=$2&subcat=$3&subsubcat=$4 [L]

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.

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