【问题标题】:Apache2 - mod_rewrite and .htacess - Not working 404 rule when i add other rulesApache2 - mod_rewrite 和 .htaccess - 当我添加其他规则时,404 规则不起作用
【发布时间】:2015-07-14 17:11:14
【问题描述】:

这是我当前的 .htaccess 文件:

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]+\s([^\s]+)\.php\s 
RewriteRule .* %1.html [R=301,L]
RewriteRule ^(.*)\.html$ $1.php 

ErrorDocument 404 /page-404.html 

像这样,当我打开以 .php 结尾的地址时,它会自动重定向到同一个地址,但以我想要的 .html 结尾。

当我删除这些行时:

RewriteEngine On

ErrorDocument 404 /page-404.html 

然后我尝试打开不存在的目录,我将 404 错误重定向到我想要的页面。当我添加我发布的第一个代码中显示的附加行时,问题就出现了。

问题出在哪里,为什么 404 错误重定向在我的第一个示例中不起作用?

提前致谢!

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite redirect


    【解决方案1】:

    有这样的规则:

    ErrorDocument 404 /page-404.html
    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} ^[A-Z]+\s([^\s]+)\.php\s [NC]
    RewriteRule .* %1.html [R=301,L]
    
    # make sure .php file exists for the requested .html file
    RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
    RewriteRule ^(.+?)\.html$ $1.php [L,NC]
    

    【讨论】:

      猜你喜欢
      • 2018-02-03
      • 1970-01-01
      • 2017-07-16
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2015-07-27
      相关资源
      最近更新 更多