【问题标题】:Redirection Htaccess without a trailing slash不带斜杠的重定向 Htaccess
【发布时间】:2018-04-26 00:43:51
【问题描述】:

我想将所有带有 google 引用的尾部斜杠的 URL 重定向到没有尾部斜杠的新 URL。

示例: http://example.com/toto/ ===> 到http://example.com/toto

警告我已经重写了规则以避免 .html 扩展名!

这是我现有的代码:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteCond %{REQUEST_URI} (.+)$
RewriteRule ^ %1 [R=301,L]
</IfModule>

我收到以下错误:

Not Found

The requested URL /example/.html was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

【问题讨论】:

    标签: .htaccess url-redirection


    【解决方案1】:

    本主题的目的是找到一种解决方案,对 google 等搜索引擎列出的现有 URL 进行重定向,以便在完全刷新网站后不丢失当前的 SEO(Wordpress 到引导程序)。

    第一个目标是将所有 url 流量,如 http://example.com/toto/ (google) ===> 重定向到 http://example.com/toto

    不幸的是,我明白,为了不丢失我在 google 上的现有列表(站点地图和机器人),进行反向操作更有效

    所以我决定审查所有代码,以便通过删除 html 扩展名并在每个 url 末尾添加一个斜杠来获得以下结果:

    http://example.com/toto.html ===> 到http://example.com/toto ==> http://example.com/toto/

    下面是我用来实现这一点的代码 HTaccess:

    RewriteEngine On
    RewriteBase /
    
    # To externally redirect /dir/foo.html to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
    RewriteRule ^ %1/ [R,L]
    
    # add a trailing slash at the end  /dir/foo/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule . %{REQUEST_URI}/ [L,R=301]
    
    # delete the extension .html 
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.*?)/?$ $1.html [L,QSA]
    

    我还在每个 html 页面中添加了下面的代码行来强制执行它:

    <base href="http://example.com/">
    

    希望能帮助任何处于相同情况的读者/编码人员

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-22
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 2016-10-29
      • 1970-01-01
      相关资源
      最近更新 更多