尾随斜线301重定向问题何时?在网址

时间:2016-01-27 08:13:21

标签: apache .htaccess redirect mod-rewrite trailing-slash

htaccess | 301重定向| URI问题

我最近将此代码应用于我的htaccess以在我的网址上强制执行斜杠:

#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的末尾添加/非常有效,除了那些?在URL的中间。如下所示:

https://www.example.com/list-of-all-objects/search?keywords=test

这个包含上述当前代码的网址会在加载时更改为:https://www.example.com/list-of-all-objects/search/?keywords=test

如果它应该如下所示:https://www.example.com/list-of-all-objects/search?keywords=test/

我很快就了解到我申请的所有代码都没有用,原因有两个。

  1. 他们没有执行/ - 他们只是让它成为可能,如果它被阻止
  2. 术语REQUEST_URI或$ 1或$ 2都不包含?keyword =(实际上我找不到任何指向URL的这一部分,因此我无法正确包含它)< / LI>

    无论我做什么 - 我都找不到?或者在它之前放置的任何东西。 HOW 我是否在之前没有应用尾部斜杠?关键字搜索但在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>
    

1 个答案:

答案 0 :(得分:2)

Query_string不属于RewriteRule中的匹配项。

要向查询字符串添加尾部斜杠,可以使用:

RewriteCond %{QUERY_STRING} ([^/]+)$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1/ [R,L]

编辑:

正如我已经说过的,查询字符串不能由RewriteRule指令直接处理,因此您需要一个单独的规则来向查询字符串添加尾部斜杠。

下面的示例为网址的两个部分添加了一个尾部斜杠。

  RewriteEngine on

  #Add a trailing slash to uri
  RewriteCond %{REQUEST_URI} !/$
  RewriteRule ^ %{REQUEST_URI}/ [NC,L,R]
  #Add a trailing slash to query strings
  RewriteCond %{QUERY_STRING} ([^/]+)$ [NC]
 RewriteRule ^ %{REQUEST_URI}?%1/ [R,L]

这将更改以下网址:

或者

(希望,这有帮助!)