htaccess 问题:从 URL 查询中删除 1(!) 参数

发布于 2024-09-14 10:34:12 字数 188 浏览 3 评论 0原文

我的网址看起来像

page.html?L=0&a=1&b=2&c=6 或 page.html?a=1&b=2&L=0&c=6 或 page.html?a=1&b=2&c=6&L=0 我需要删除 L=0,其中 L 可以是 0 或 1。但我需要保留所有其他参数。这怎么能做到呢?

my URL looks like

page.html?L=0&a=1&b=2&c=6 OR
page.html?a=1&b=2&L=0&c=6 OR
page.html?a=1&b=2&c=6&L=0
I need to delete the L=0, where L can be either 0 or 1. But I need to keep all other params. How can this be done?

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

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

发布评论

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

评论(3

单身情人 2024-09-21 10:34:12

试试这个规则:

RewriteCond %{QUERY_STRING} ^(([^&]*&+)*)L=[01](&+(.*))?$
RewriteRule ^page\.html$ %{REQUEST_URI}?%1%4 [L,R=301]

Try this rule:

RewriteCond %{QUERY_STRING} ^(([^&]*&+)*)L=[01](&+(.*))?$
RewriteRule ^page\.html$ %{REQUEST_URI}?%1%4 [L,R=301]
爱的那么颓废 2024-09-21 10:34:12

好吧,我在 http://gskinner.com/RegExr/ 上玩了很多。

我根本找不到适用于所有这些情况的正则表达式:

domain.com/login.html? p1=1&p2=2&L=0&P3=3
domain.com/login.html?p1=1&p2=2&L=1&P3=3
domain.com/login.html?p1=1&p2=2&L=0
域名.com/login.html?&L=0
域名.com/login.html?L=0
domain.com/login.html?L=0&p1=1&p2=2&P3=3

最后的最佳猜测是 (.)(L=[a-zA-Z0-9]&?)( &?)(.)>>>> $1$3$4

这导致了

domain.com/login.html?p1=1&p2=2&P3=3
域名.com/login.html?p1=1&p2=2&P3=3
domain.com/login.html?p1=1&p2=2&
域名.com/login.html?&
域名.com/login.html?
domain.com/login.html?p1=1&p2=2&P3=3

我尝试的是:
- 如果 L 参数位于中间,则将其剪掉
- 如果 L 参数位于单独参数字符串的末尾,则将其截断
- 切断 L 参数和“?”如果它是唯一的参数

你能提供更多提示吗?

Ok, I played around a lot on http://gskinner.com/RegExr/.

I simply can't find a regex that works on all these cases:

domain.com/login.html?p1=1&p2=2&L=0&P3=3
domain.com/login.html?p1=1&p2=2&L=1&P3=3
domain.com/login.html?p1=1&p2=2&L=0
domain.com/login.html?&L=0
domain.com/login.html?L=0
domain.com/login.html?L=0&p1=1&p2=2&P3=3

Last best guess was (.)(L=[a-zA-Z0-9]&?)(&?)(.) >>> $1$3$4

This resulted in

domain.com/login.html?p1=1&p2=2&P3=3
domain.com/login.html?p1=1&p2=2&P3=3
domain.com/login.html?p1=1&p2=2&
domain.com/login.html?&
domain.com/login.html?
domain.com/login.html?p1=1&p2=2&P3=3

What I try is:
- Cut out the L param if it is in the middle
- Cut off the L param if it is at the end of a loner param string
- Cut off the L param AND "?" if it is the only param

Can you provide some more hints?

小红帽 2024-09-21 10:34:12

好吧,又是一个头痛,我终于到了。我只是在此处发布此内容,以防万一我

  • 错过了某些内容
  • ,也许这可以帮助处于类似情况的其他人

。htaccess 规则

# Rewrite Typo3 links with multilanguage Param "L"
RewriteCond %{QUERY_STRING} (.*)(^L=[a-zA-Z0-9]+&?|^&L=[a-zA-Z0-9]+&|&L=[a-zA-Z0-9]+)(&?.*)
RewriteRule (.*) %{REQUEST_URI}?%1%3 [L,R=301]

说明

该规则尝试涵盖以下所有情况

  • domain.com/login.html?p1=1&p2=2&L =abc&P3=3
  • 域名.com/login.html
  • ?p1=1&p2=2&L= def 域名.com
  • /login.html?&L=eh1域名.com /login.html?&L= asdfasd&p1=1&p2=
  • 2domain.com/login.html?L=0domain.com/login.html
  • ?

L=1& amp;p1=1&p2=2&P3=3 据我所知,正则表达式的工作原理如下:

  1. (.*) 检查以下 L 参数选项之前是否有任何内容
  2. 第二个 ( ) 对组包含 3 个替代方案/案例L 参数可以出现在查询字符串中,
  3. 第三个 (.*) 仅涵盖 L 参数后面可能出现的所有内容

,因此替换只需要 %{REQUEST_URI}?%1% 3.。这将剪切具有不同 L 参数选项的第二个 ( ) 组。

最终结果

URL 查询字符串将被删除 L 参数,但保留其他所有内容。

有什么意见吗?如果这对您也有价值,请随意投票。

感谢所有帮助我找到正确解决方案的人!

Ok, another headache and I am finally there. I just post this here in case I

  • missed something
  • maybe this can help someone else in a similar situation

.htaccess rule

# Rewrite Typo3 links with multilanguage Param "L"
RewriteCond %{QUERY_STRING} (.*)(^L=[a-zA-Z0-9]+&?|^&L=[a-zA-Z0-9]+&|&L=[a-zA-Z0-9]+)(&?.*)
RewriteRule (.*) %{REQUEST_URI}?%1%3 [L,R=301]

Explanation

The rule tries to cover all the following cases

  • domain.com/login.html?p1=1&p2=2&L=abc&P3=3
  • domain.com/login.html?p1=1&p2=2&L=def
  • domain.com/login.html?&L=eh1
  • domain.com/login.html?&L=asdfasd&p1=1&p2=2
  • domain.com/login.html?L=0
  • domain.com/login.html?L=1&p1=1&p2=2&P3=3

As far as my knowledge goes, the regex works as follow:

  1. (.*) checks if there is anything before the following L-param option
  2. The second ( )-pair group contains 3 alternatives/cases on how the L-param could be present in the query string
  3. the third (.*) simply covers everthing that might follow the L-param

Therefor the replacement only needs %{REQUEST_URI}?%1%3. This will cut out the second ( ) group with the different L-param options.

End result

The URL query string will be stripped of the L-param, but leave everything else.

Any comments? Feel free to vote if this is valuable for you, too.

Thanks to all who helped me find the correct solution!

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