【问题标题】:Add Trailing Slash to Posts在帖子中添加斜杠
【发布时间】:2021-05-15 23:59:34
【问题描述】:

我正在使用以下 .htaccess 代码添加尾部斜杠,但主页除外。

## Base Redirects ##

# Turn on Rewrite Engine
RewriteEngine On

# Include trailing slash on non-filepath urls
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ https://hamilekadin.net/$1/ [R=301,L]

# Remove trailing slash from directory
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/$ https://hamilekadin.net/$1 [R=301,L]

# Force HTTPS and remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{HTTPS} off  
RewriteRule ^(.*)$ https://hamilekadin.net/$1 [R=301,L]

我想要非 www url、https 协议和 post 和 page url 后面的斜杠。

使用这个 .htaccess,我在类别、页面、帖子上遇到 404 错误

我的永久链接类型也是:/%postname%/

【问题讨论】:

    标签: regex wordpress .htaccess redirect mod-rewrite


    【解决方案1】:

    这是斜杠的代码

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

    对于非www url,有两种选择

    选项 1:

    您需要运行 SQL 查询以从 url 中删除 www

    选项 2

    在.htacess中添加以下代码

    RewriteCond %{HTTP_HOST} ^www. yourdomain\.com [NC]
    RewriteRule ^(.*)$ https://yourdomain.com $1 [L,R=301,NC]
    

    据我说,删除 www 最可取的方式是选项 1,您只需要运行查询即可。

    【讨论】:

    • 尾部斜杠代码不起作用。 @a-shah
    • 你改了url是什么错误?
    • $1 后面应该有“/”,不是吗?有了这些,就会出错。它没有将 http 重定向到 https,并且帖子上也出错。
    • 我第二行缺少 https url
    • 不工作。在 httpstatus.io 上查看
    猜你喜欢
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2012-01-12
    • 2015-02-06
    • 1970-01-01
    相关资源
    最近更新 更多