【问题标题】:Redirect 301 from non trailing slash to trailing slash at the end将 301 从非斜杠重定向到末尾的斜杠
【发布时间】:2016-08-07 00:56:06
【问题描述】:

我已经在子目录 domain.com/blog/ 中安装了 Wordpress 尾部斜杠的问题,Wordpress 帖子为 domain.com/blog/post-name 和 domain.com/blog/post-name/ 两个版本提供了 200 OK http

我想在末尾强制使用斜杠并将 301 从非斜杠重定向到末尾的斜杠

所以斜线版本只给出 200 OK

我的代码

# Force Trailing Slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]

【问题讨论】:

    标签: php wordpress .htaccess redirect mod-rewrite


    【解决方案1】:

    您的代码的问题在于正则表达式:^[^/]+$

    这个正则表达式匹配没有任何斜杠的每个字符串,所以像 domain.com/blog 这样的字符串不会匹配。实现目标的最佳方式如下:

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

    如果你使用默认的wordpress htaccess file,你应该这样做:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
    
        # Force Trailing Slash
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_URI} ^(.*)[^/]{1}$
        RewriteRule (.*) $1/ [L,R=301]
    
    
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
    
    </IfModule>
    # END WordPress
    

    【讨论】:

    • 谢谢它的工作就像一个魅力,请看看那个问题stackoverflow.com/questions/38733574/…谢谢
    • 静态网页现在的问题,它重定向到 domain.com/articles 而不是 domain.com/articles/ 其他帖子的详细信息在高级表示感谢
    • 抱歉,我没有理解静态网页的问题。能多说一点吗?
    • 请检查下面的链接,我已经解释了那里的一切stackoverflow.com/questions/38733574/…
    猜你喜欢
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多