【问题标题】:Why are my URLs not being rewritten with an ending slash?为什么我的 URL 没有用结束斜杠重写?
【发布时间】:2017-01-01 07:45:12
【问题描述】:

当我在浏览器地址栏中以 https://myurl.com/path 的形式输入 URL 时,我希望重写为 https://myurl.com/path/

在.htaccess中,我的重写规则如下:

#RewriteEngine On 
#RewriteCond %{HTTP_HOST} !^myurl.com$
#RewriteRule ^(.*)$ http://myurl.com/$1 [R=301,L]
#RewriteCond %{SERVER_PORT} 80 
#RewriteRule ^(.*)$ https://myurl.com/$1 [R,L]
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://myurl.com%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

【问题讨论】:

    标签: wordpress .htaccess url-rewriting


    【解决方案1】:

    在过去的几天里,我一直在对此进行大量研究。看起来你很近。请尝试以下操作:

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

    RewriteCond 将检查以确保没有具有该名称的文件,如果没有,则执行 RewriteRule。比手动列出扩展更具有前瞻性!

    这是来自以下问答的复制/粘贴:我对 .htaccess 不是很好,但就像我说的,过去几天我一直在研究它。 .htaccess Rewrite to Force Trailing Slash at the end

    [R=301] 还将向搜索引擎的 url 添加 301 重定向,因此他们知道将来会查看页面的斜线版本 - 这有助于减少重复内容并整合分析信息。

    希望对您有所帮助。

    【讨论】:

    • 感谢@MrMr 的工作:) 我没有亲自将规则添加到这个 .htaccess 文件中,其他人做了,所以我不能因为接近解决方案而受到赞扬。我我不确定为什么要添加 .html 规则,所有页面的格式都是 myurl/path/page。您是否建议我仅替换以下行:RewriteCond %{REQUEST_FILENAME}.html -fRewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
    • 我现在遇到的一个问题是我的 XML 站点地图现在有一个斜杠,即 /sitemap.xml/。有趣的是 - 值得庆幸的是 - pdf 文件没有尾部斜杠。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2013-01-30
    • 2013-01-07
    • 2017-02-25
    相关资源
    最近更新 更多