问题原文
原帖链接 · 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&s [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&s [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]
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
