1

目前我有一个 htaccess 重写,它允许一个斜杠或没有斜杠。

RewriteRule ^trams/([a-zA-Z\-]+)/([a-zA-Z0-9\-]+)/([0-9-]+)/?$ trams/more_details.php?id=$3&tram=$1 [QSA,NC,L]

对于上面的这个特定重写,我正在尝试更改它,以便如果URL 有斜杠,它 301 会重定向到没有斜杠的版本,但我不确定要在上面的行中添加什么?

更复杂的是,在 htaccess 文件的顶部有一个代码块,它在整个网站的某些 URL 中添加了一个斜杠(这是网站其余部分的要求,抱歉不一致),所以我还需要弄清楚我需要在下面的块中添加什么行以忽略上述规则....

# If requested resource does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# and does not end with a period followed by a filetype
RewriteCond %{REQUEST_URI} !\..+$
# and does not end with a slash
RewriteCond %{REQUEST_URI} !/$
# then add a trailing slash and redirect
RewriteRule (.*) http://%{HTTP_HOST}/$1/ [R=301,L]
4

1 回答 1

1

要从给定的 URL 模式中删除尾部斜杠:

RewriteRule ^(trams/[\w-]+/[\w-]+/[\d-]+)/$ $1 [R=301,L]

# If requested resource does not exist as a file 
RewriteCond %{REQUEST_FILENAME} !-f 
# and does not end with a period followed by a filetype 
RewriteCond %{REQUEST_URI} !\..+$ 
# URL is not the one we don't want a trailing slash
RewriteCond %{REQUEST_URI} !^/trams/[\w-]+/[\w-]+/[\d-]+$ 
# then add a trailing slash and redirect 
RewriteRule ^(.+?[^/])$ http://%{HTTP_HOST}/$1/ [R=301,L]

PS:确保将此规则置于所有其他现有规则之上。

于 2013-09-11T15:41:19.523 回答