【发布时间】:2016-05-04 01:58:18
【问题描述】:
htaccess | 301 重定向 | URI 问题
我最近将此代码应用于我的 htaccess 以在我的 URL 上强制使用尾部斜杠:
#Trailing backslash
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
该代码非常适合在 MOST URL 的末尾添加 /,除了那些带有 ?在网址中间。如下:
https://www.example.com/list-of-all-objects/search?keywords=test
带有上述当前代码的此 URL 在加载时将更改为:https://www.example.com/list-of-all-objects/search/?keywords=test
什么时候应该是这样的:https://www.example.com/list-of-all-objects/search?keywords=test/
我很快了解到我应用的所有代码都不起作用,原因有两个。
- 他们不强制执行 / - 他们只是在它被阻止时使其成为可能
- 术语 REQUEST_URI 或 $1 或 $2 都不包含 ?keyword=(事实上,我找不到任何涉及 URL 的这一部分的内容,因此我无法正确包含它)
无论我做什么 - 我都无法获得 ?或它后面的任何东西都放在 / 之前。 如何在 ? 之前不应用尾部斜杠?在关键字搜索但在 URL 的末尾并继续在所有其他 URL 的末尾应用?
其他之前尝试过的方法:
# Force Trailing Slash
enter code hereRewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
在这里找到:.htaccess Rewrite to Force Trailing Slash at the end
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
在这里找到:Htaccess: add/remove trailing slash from URL
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
</IfModule>
在这里找到:http://www.paulund.co.uk/using-htaccess-to-force-trailing-slash
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
在这里找到:http://ralphvanderpauw.com/seo/how-to-301-redirect-a-trailing-slash-in-htaccess/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
在这里找到:http://enarion.net/web/htaccess/trailing-slash/
# Trailing slash check
# Don't fix direct file links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
在这里找到:https://moz.com/community/q/trailing-slash-at-end-of-url
# Always append a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [R=301,L]
在这里找到:https://craftcms.stackexchange.com/questions/9501/force-trailing-slash-on-urls
以下是我完整的 htaccess 文件内容:
RewriteEngine On
#Trailing backslash
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
#HTTPS Redirects
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* [QSA,L]
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access plus 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
【问题讨论】:
标签: apache .htaccess redirect mod-rewrite trailing-slash