【问题标题】:.htaccess 301 Redirect non slash domains and slash domain to .html.htaccess 301 将非斜线域和斜线域重定向到.html
【发布时间】:2013-04-27 16:07:30
【问题描述】:

我正在尝试在我的 .htaccess 中添加一些代码,以将斜杠和非斜杠 url 重定向到 .html 的所有 url,除了我的主页。

例如 www.mydomain.com/cat/ 和 www.mydomain.com/cat 应该重定向到 www.mydomain.com/cat.html

我已设法将以下内容添加到我的 .htaccess 中,它将 www.mydomain.com/cat 重定向到正确的位置 www.mydomain.com/cat.html 但需要一些有关如何使斜线版本重定向到 .htaccess 的帮助。 html页面

RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]

我的整个 .htaccess 看起来像这样,如果有人根据上述内容对它的外观有任何建议,将不胜感激。

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^xxx.xxx.xxx.xx [nc,or]
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

RewriteCond %{THE_REQUEST} ^.*/index.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L] 


RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]


DirectoryIndex index.html index.php

<IfModule mod_rewrite.c>
RewriteEngine on
# Pleas note that RewriteBase setting is obsolete use it only in case you experience  some problems with SEO addon.
# Some hostings require RewriteBase to be uncommented
# Example:
# Your store url is http://www.yourcompany.com/store/cart
# So "RewriteBase" should be:
# RewriteBase /store/cart
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]

</IfModule>

已解决: 我刚刚补充了:

RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$  /$1 [R=301,L]

【问题讨论】:

    标签: .htaccess redirect seo slash


    【解决方案1】:

    单独的规则似乎对您有用,但我认为您可以将其简化为一条规则,并带有可选的斜杠。您的规则将斜杠重定向到无斜杠,然后再次重定向到 .html。使用一条规则,您将只有一个重定向。

    这具有检查它是否不是文件或文件夹的标准 RewriteCond,因此如果它已经是一个,它不会继续重定向 .html。那么,ReweriteRule 中的\? 是一个可选的斜线。

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/?$ http://mydomain.com/$1.html [R=301,L]
    

    如果这些都在您的域中,您可以从结果中省略它:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/?$ /$1.html [R=301,L]
    

    另外,请注意,无论您是否有意,这都会捕获并处理子文件夹。例如。, www.mydomain.com/animals/cat/ 将重定向到 www.mydomain.com/animals/cat.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      相关资源
      最近更新 更多