【问题标题】:Trailing Slash 301 Redirect Issue When ? Is In URL尾随斜杠 301 重定向问题何时?在网址中
【发布时间】: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/

我很快了解到我应用的所有代码都不起作用,原因有两个。

  1. 他们不强制执行 / - 他们只是在它被阻止时使其成为可能
  2. 术语 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


    【解决方案1】:

    Query_string 不属于 RewriteRule 中的匹配项。

    要在查询字符串中添加斜杠,可以使用:

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

    编辑:

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

    下面的示例在 url 的两个部分都添加了一个斜杠。

      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]
    

    这将改变以下网址:

    或者

    (希望,这会有所帮助!)

    【讨论】:

    • 惊人的答案,但这只会对我强制问题 URL 上的斜杠,并且不适用于所有其他人。有没有办法做到这两点。这就是我的 htaccess 尾部斜杠部分现在的外观以及您建议的更改 #Trailing backslash RewriteBase / RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ https://www.reeffree.com.au/$1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !(.*)/$ RewriteCond %{QUERY_STRING} ([^/]+)$ [NC] RewriteRule ^ %{REQUEST_URI}?%1/ [R,L]
    • @Trent 为此,您需要使用 2 个单独的规则。查看编辑。
    • 感谢您的反馈。我理解并同意需要两个语句,但同时具有这两个语句会导致 URL 的规则不带 ?适用于带有 ?同样,它最终会这样做:example.com/list-of-all-objects/search/?keywords=test Is there a way to use an IF statement
    • 是否可以使用 if 语句?
    猜你喜欢
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 2011-02-11
    相关资源
    最近更新 更多