大网数据广告活动
网站运营与故障问答

Rewrite URL with one unnecessary parameter

· 52CE 我爱测

I have the following URL examples. Example one is a URL without "amps" parameter: https://multilang:8890/uk/seconddoc.html/ there I have a 404 error. And an other one with that amps parameter that works just fine: https://multila…

问题原文

原帖链接 · Bogdan Onyshenko · CC BY-SA 4.0

I have the following URL examples. Example one is a URL without "amps" parameter: https://multilang:8890/uk/seconddoc.html/ there I have a 404 error. And an other one with that amps parameter that works just fine: https://multilang:8890/uk/seconddoc.html/amps The amps parameter can be present or not present. Here is my .htaccess rule for overwriting this

RewriteRule ^(ru|en|uk|)/(.*)/(amps)$ index.php?cultureKey=$1&q=$2&amps [QSA]

How can I rewrite those URLs so that example 1 and example 2 will direct to a same page?

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 4.0

You need to make the "amps" in the URL optional by adding a ? to the regex in the correct spot. Also because regex are greedy, you shouldn't accept slashes in the middle of your URL. Use [^/]* instead of .*:

RewriteRule ^(ru|en|uk|)/([^/]*)/(amps)?$ index.php?cultureKey=$1&q=$2&amps [QSA]

You might also want to not pass "amps" as a parameter if it isn't in the URL. To do that, you could use a third capturing variable ($3):

RewriteRule ^(ru|en|uk|)/([^/]*)/(amps)?$ index.php?cultureKey=$1&q=$2&$3 [QSA]

You might also want to make the trailing slash optional so that https://multilang:8890/uk/seconddoc.html works:

RewriteRule ^(ru|en|uk|)/([^/]*)(/(amps)?)?$ index.php?cultureKey=$1&q=$2&$4 [QSA]

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2022-04-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。