gpt4 book ai didi

.htaccess 301 将非斜杠域和斜杠域重定向到 .html

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:45:59 25 4
gpt4 key购买 nike

我正在尝试向我的 .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 但需要一些帮助来了解如何将斜杠版本重定向到 . 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]

最佳答案

单独的规则似乎对您有用,但我认为您可以将其简化为一个规则,使用可选的斜线。您的规则将斜杠重定向到无斜杠,然后再次重定向到 .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

关于.htaccess 301 将非斜杠域和斜杠域重定向到 .html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16355773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com