【问题标题】:How to target all .html URLS except index.html in htaccess如何在 htaccess 中定位除 index.html 之外的所有 .html URL
【发布时间】:2017-07-21 11:22:46
【问题描述】:

我目前在我的 Wordpress htaccess 文件中有这个:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.html?$ / [NC,R,L]
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
    </IfModule>

    #RedirectMatch 301 (.*)\.html$ $1/

我正在尝试将所有 .html 重定向到没有任何文件扩展名的 URL,即 www.example.com/about.html 到 www.example.com/about/。

代码在 /index.html 重定向到 / 时工作正常

上面的注释代码 (RedirectMatch 301 (.*)\.html$ $1/) 应该适用于将 .html 重定向到漂亮的 URL,但它也适用于 index.html,而不是将其重定向到 / 而是将其重定向到 /index

有没有办法让我压缩我的代码并做我想做的事?

【问题讨论】:

    标签: wordpress .htaccess url permalinks


    【解决方案1】:

    像这样:

    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^index\.html?$ / [NC,R,L]
    
    # redirect every .html except index.html
    RewriteRule ^(?!index\.html$)(.+)\.html?$ /$1 [NC,R,L]
    
    RewriteRule ^index\.php$ - [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    

    【讨论】:

      猜你喜欢
      • 2021-05-01
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多