试图将ErrorDocuments添加到HTACCESS但是有重写冲突

时间:2015-06-26 23:41:44

标签: php regex apache .htaccess mod-rewrite

我遇到这个问题。我希望有一个用户获取的错误代码(400,401,403,404,500)路由到根目录中名为error.php的页面。

我已经研究过我可以上网的内容,但无济于事我发现任何运气。我的问题有点独特,我对htaccess文件的了解非常基本。

以下是代码:

## Route error pages
ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php

Options +FollowSymlinks -Multiviews -Indexes
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ $1.php
RewriteRule ^item/(.*)$ ./itemlookup.php?search=$1
RewriteRule ^recipe/(.*)$ ./recipelookup.php?search=$1
RewriteRule ^secret/recipe/(.*)$ ./secret/recipelookup.php?search=$1
RewriteRule ^api/json/idbyname/(.*)$ ./api/json/getidbyname.php?id=$1 [QSA]
RewriteRule ^api/csv/idbyname/(.*)$ ./api/csv/getidbyname.php?id=$1 [QSA]
RewriteRule ^api/json/item/(.*)$ ./api/json/items.php?id=$1 [QSA]
RewriteRule ^api/csv/item/(.*)$ ./api/csv/items.php?id=$1 [QSA]
RewriteRule ^api/json/forge/(.*)$ ./api/json/mysticforge.php?id=$1 [QSA]
RewriteRule ^api/csv/forge/(.*)$ ./api/csv/mysticforge.php?id=$1 [QSA]
RewriteRule ^api/json/history/(.*)$ ./api/json/tradehistory.php?id=$1 [QSA]
RewriteRule ^api/csv/history/(.*)$ ./api/csv/tradehistory.php?id=$1 [QSA]
RewriteRule ^api/json/history-daily/(.*)$ ./api/json/tradehistorydaily.php?id=$1 [QSA]
RewriteRule ^api/csv/history-daily/(.*)$ ./api/csv/tradehistorydaily.php?id=$1 [QSA]
RewriteRule ^api/json/recipe/(.*)$ ./api/json/craftingrecipes.php?id=$1 [QSA]
RewriteRule ^api/csv/recipe/(.*)$ ./api/csv/craftingrecipes.php?id=$1 [QSA]
RewriteRule ^api/json/exchange/(.*)$ ./api/json/gemexchange.php [QSA]
RewriteRule ^api/csv/exchange/(.*)$ ./api/csv/gemexchange.php [QSA]

#Gzip Compression, Saves on bandwidth
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript application/vnd.ms-fontobject application/x-font-ttf image/svg+xml
</ifmodule>
#End Gzip

## EXPIRES CACHING - 1 Month ##
<FilesMatch "\.(gif|png|jpg|jpeg|woff|woff2|svg)$">
ExpiresActive on 
ExpiresDefault "access plus 1 month"
</FilesMatch>
<FilesMatch "^(jquery-1\.9\.1\.min\.js)$">
ExpiresActive on 
ExpiresDefault "access plus 1 year"
</FilesMatch>

我的用户将正确查看现有页面。但任何404页面错误及其相似之处都会重新路由到500内部服务器错误(没有错误页面路由)。

简而言之,ErrorDocument行没有工作。所有帮助当然感激不尽!

此致 RebornGeek

1 个答案:

答案 0 :(得分:1)

  1. 最后保持.php添加规则。
  2. 仅在匹配文件存在时才添加.php
  3. 用以下内容替换您的规则:

    ## Route error pages
    ErrorDocument 400 /error.php
    ErrorDocument 401 /error.php
    ErrorDocument 403 /error.php
    ErrorDocument 404 /error.php
    ErrorDocument 500 /error.php
    
    Options +FollowSymlinks -Multiviews -Indexes
    RewriteEngine on
    
    RewriteRule ^(index|error)\.php$ - [L,NC]
    
    RewriteRule ^item/(.*)$ itemlookup.php?search=$1 [L,QSA]
    RewriteRule ^recipe/(.*)$ recipelookup.php?search=$1 [L,QSA]
    RewriteRule ^secret/recipe/(.*)$ secret/recipelookup.php?search=$1 [L,QSA]
    RewriteRule ^api/json/idbyname/(.*)$ api/json/getidbyname.php?id=$1 [QSA,L]
    RewriteRule ^api/csv/idbyname/(.*)$ api/csv/getidbyname.php?id=$1 [QSA,L]
    RewriteRule ^api/json/item/(.*)$ api/json/items.php?id=$1 [QSA,L]
    RewriteRule ^api/csv/item/(.*)$ api/csv/items.php?id=$1 [QSA,L]
    RewriteRule ^api/json/forge/(.*)$ api/json/mysticforge.php?id=$1 [QSA,L]
    RewriteRule ^api/csv/forge/(.*)$ api/csv/mysticforge.php?id=$1 [QSA,L]
    RewriteRule ^api/json/history/(.*)$ api/json/tradehistory.php?id=$1 [QSA,L]
    RewriteRule ^api/csv/history/(.*)$ api/csv/tradehistory.php?id=$1 [QSA,L]
    RewriteRule ^api/json/history-daily/(.*)$ api/json/tradehistorydaily.php?id=$1 [QSA,L]
    RewriteRule ^api/csv/history-daily/(.*)$ api/csv/tradehistorydaily.php?id=$1 [QSA,L]
    RewriteRule ^api/json/recipe/(.*)$ api/json/craftingrecipes.php?id=$1 [QSA,L]
    RewriteRule ^api/csv/recipe/(.*)$ api/csv/craftingrecipes.php?id=$1 [QSA,L]
    RewriteRule ^api/json/exchange/(.*)$ api/json/gemexchange.php [QSA,L]
    RewriteRule ^api/csv/exchange/(.*)$ api/csv/gemexchange.php [QSA,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]
    
    #Gzip Compression, Saves on bandwidth
    <ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript application/vnd.ms-fontobject application/x-font-ttf image/svg+xml
    </ifmodule>
    #End Gzip