404文件的.htaccess循环问题

时间:2015-09-29 18:39:51

标签: php .htaccess loops mod-rewrite http-status-code-404

我一直在阅读手册和本网站以获得指导,但我需要一些帮助。

我当前的htaccess文件执行以下操作:

  1. 万维网。被迫
  2. php extensions hidden
  3. 在root上添加正斜杠 和目录文件
  4. 问题是不存在的php文件(404)属于从.php /到/的PHP循环。不确定如何解决问题。

    如何在不存在的文件上停止循环?

    RewriteEngine on 
    
    # CANONICAL URL
    RewriteCond %{HTTP_HOST} ^example.com [NC] 
    RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301] 
    
    #REDIRECT INDEX.PHP TO ROOT
    RewriteCond %{THE_REQUEST} ^.*/index.php 
    RewriteRule ^(.*)index.php$ /$1 [R=301,L] 
    
    #REMOVE .PHP EXTENSION
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1/ [R=301,L]
    
    #ADD TRAILING SLASH 
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule . %{REQUEST_URI}/ [L,R=301]
    
    #INTERNALLY FORWARD (example: /dir/foo to /dir/foo.php)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*?)/?$ $1.php [L]
    
    #PAGINATION LINKS
    RewriteRule   ^(.*)/$ $1.php
    RewriteRule ^(.*)/([0-9]+)$  $1.php?page=$2
    

1 个答案:

答案 0 :(得分:0)

RewriteEngine On上方添加此行:

ErrorDocument 404 default

这将重置Apache config中设置的任何404处理程序。

在完全清除浏览器缓存后对此进行测试。

完整.htaccess:

ErrorDocument 404 default
RewriteEngine on 

# CANONICAL URL
RewriteCond %{HTTP_HOST} ^example\.com$ [NC] 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301] 

#REDIRECT INDEX.PHP TO ROOT
RewriteCond %{THE_REQUEST} /index\.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L] 

#REMOVE .PHP EXTENSION
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]

#ADD TRAILING SLASH 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]

#PAGINATION LINKS
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^./]+)/([0-9]+)/?$  $1.php?page=$2 [L,QSA]

#INTERNALLY FORWARD (example: /dir/foo to /dir/foo.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]