首页 > 解决方案 > .htaccess rule fails on server if using optional trailing slash

问题描述

I have the following set of .htaccess rules

RewriteRule ^$ /?lang=en [L,QSA]
RewriteRule ^(en|fr)/?$ /?lang=$1 [L,QSA]
RewriteRule ^(en|fr)/(.*?)/?$ /?lang=$1&page=$2 [L,QSA]
RewriteRule ^(.*?)/?$ /?lang=en&page=$1 [L,QSA]

The last rule is the newest to be implemented and is meant to be a fallback for if no language code was supplied to attempt serving the page using the default language.

The first 3 rules work perfectly in production and on local but when introducing the 4th rule it breaks. But it only breaks when the trailing slash is optional... if the trailing slash is omitted completely it is broken and if it is optional it is broken but if it is enforced it works. However this of course has the side effect of requiring a trailing slash in order for the rule to be met.

RewriteRule ^(.*?)/?$ /?lang=en&page=$1 [L,QSA] DOES NOT WORK (500 ERROR)
RewriteRule ^(.*?)$ /?lang=en&page=$1 [L,QSA] DOES NOT WORK (500 ERROR)
RewriteRule ^(.*?)/$ /?lang=en&page=$1 [L,QSA] WORKS BUT REQUIRES TRAILING SLASH

Whats going on? TIA

标签: regex.htaccessurl-rewriting

解决方案


推荐阅读