duanbai1370 2016-08-07 00:56
浏览 70
已采纳

在结尾处将301从非尾部斜杠重定向到尾部斜杠

I've installed Wordpress in subdirectory domain.com/blog/ The problem with the trailing slash , Wordpress posts gives 200 OK http for the two versions domain.com/blog/post-name and domain.com/blog/post-name/

i want to force trailing slash at the end and redirect 301 from non slash to trailing slash at the end

so trailing slash version gives 200 OK ONLY

my code

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

1条回答 默认 最新

  • dongshan4518 2016-08-07 01:11
    关注

    The problem with your code is at the regex: ^[^/]+$

    This regex matches every string without ANY slash, so a string like domain.com/blog would not match. The best way to achieve your goal is like the following:

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

    If you are using the default wordpress htaccess file you should do like this:

    # 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?