【问题标题】:Redirect all the .html files to old site with htaccess使用 htaccess 将所有 .html 文件重定向到旧站点
【发布时间】:2016-03-22 16:20:57
【问题描述】:

我想将所有以 .html 结尾的文件重定向到旧站点文件夹,目前我的 htaccess 如下所示:

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / # RedirectMatch 302 /(.+?).html$ http://superawesomedomain.com/old/$1.html [L] RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>

但是在我的条件RedirectMatch 302 应用时它会到达http://superawesomedomain.com/old/old/old/old/old/old...old/old/old...old/old/old/ 直到它破坏浏览器:(

关于如何进行重定向的任何想法?

谢谢!

【问题讨论】:

  • 问题是循环,它总是找到一个html,所以继续重定向直到浏览器崩溃
  • redirectmatch 不是 mod_rewrite 的一部分,因此 [L] 对它没有任何意义。

标签: php regex apache .htaccess redirect


【解决方案1】:

您可以使用这些规则:

RewriteEngine On
RewriteBase /

RewriteRule ^((?!old/).+?\.html)$ /old/$1 [L,NC,R=302]

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
  • 不要将RedirectMatch 与其他mod_rewrite 规则一起使用
  • 当 URI 已经以 /old/ 开头时,请确保您没有重定向以停止重定向循环

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 2017-08-17
    • 2015-11-22
    • 2017-09-25
    相关资源
    最近更新 更多