我在mod_rewrite中使用errordocument指令,但问题是如果我使用相对路径重定向,我会对放在其他目录中的文件进行错误的重定向。
例如:
我的重定向是
ErrorDocument 404 /index.php?page=error&type=04
如果我访问 http://mydomain.com/a_resource_that_doesnt_exist 它重定向确定
但如果我访问 http://mydomain.com/images_folder/an_image_that_doesnt_exist 它将所有请求重定向到http://mydomain.com/images_folder/request_uri,因此当另一个目录中有文件请求时,它会失败,因为它正在使用images_folder,例如当文件位于{{时,它会尝试从http://mydomain.com/images_folder/css_folder/my_css_file.css加载css文件3}}
如果我使用此重定向
ErrorDocument 404 http://mydomain.com/index.php?page=error&type=04
它工作正常,但由于绝对路径
,仅适用于生产环境,但不适用于其他环境我尝试使用%{HTTP_HOST}
ErrorDocument 404 http://%{HTTP_HOST}/index.php?page=error&type=04
但它不起作用
此问题的任何解决方案
这是问题的.htaccess规则
Options FollowSymLinks
RewriteEngine On
##################
#SECURITY #
##################
ServerSignature Off
AddDefaultCharset UTF-8
# SQL INJECTION
RewriteRule .*(DECLARE|NVARCHAR|INSERT|INFILE|OUTFILE|LOAD_FILE|CREATE|DROP|TRUNCATE|GRANT|UNION|CREATE|DELETE|INSERT|UPDATE|SELECT|ALTER|REPLACE|SHOW|DESCRIBE|DELIMITER|PREPARE|LIKE)(\s|%20).* index.php?page=inicio [NC,L]
#FUNCTIONS
RewriteRule .*(USER|DATABASE|VERSION|SLEEP|BENCHMARK|NOW|ROW_COUNT|SCHEMA|ASCII|CAST|CONVERT|CHAR|HEX|UNHEX|INSTR|LOAD_FILE|LOCATE|POSITION|LPAD|RPAD|ORD|QUOTE|REPLACE|REVERSE|LEFT|RIGHT|SUBSTR|SUBSTRING|CONCAT|CONCAT_WS|MD5|SHA1|PASSWORD|ENCODE|COMPRESS|CALL|EXEC|IF)(\s|%20)*\(.* index.php?page=inicio [NC,L]
RewriteRule .*(@|';|\/\*|\*\/|\+|\|\||sp_password|(\s|%20)xp_).* index.php?page=inicio [NC,L]
# OTHERS
#script, php, file injection
RewriteRule .*(require|globals)(\s|%20).* index.php?page=inicio [NC,L]
RewriteRule .*(request|script|base64_encode).* index.php?page=inicio [NC,L]
#header injection
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS|HEAD) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} (libwww-perl|indy\\library) [NC]
RewriteRule .* index.php?page=inicio [NC,L]
#################################
#ERROR PAGES #
#################################
#HTTP_BAD_REQUEST
ErrorDocument 400 /index.php?page=error&tipo=01
#HTTP_UNAUTHORIZED
ErrorDocument 401 /index.php?page=error&tipo=02
#HTTP_FORBIDDEN
ErrorDocument 403 /index.php?page=error&tipo=03
#HTTP_NOT_FOUND
### -----------------> this doesn't work --------> ErrorDocument 404 http://%{HTTP_HOST}/index.php?page=error&tipo=04
ErrorDocument 404 /index.php?page=error&tipo=04
#HTTP_METHOD_NOT_ALLOWED
ErrorDocument 405 /index.php?page=error&tipo=05
#HTTP_REQUEST_TIME_OUT
ErrorDocument 408 /index.php?page=error&tipo=06
#HTTP_GONE
ErrorDocument 410 /index.php?page=error&tipo=07
#HTTP_LENGTH_REQUIRED
ErrorDocument 411 /index.php?page=error&tipo=08
#HTTP_PRECONDITION_FAILED
ErrorDocument 412 /index.php?page=error&tipo=09
#HTTP_REQUEST_ENTITY_TOO_LARGE
ErrorDocument 413 /index.php?page=error&tipo=10
#HTTP_REQUEST_URI_TOO_LARGE
ErrorDocument 414 /index.php?page=error&tipo=11
#HTTP_UNSUPPORTED_MEDIA_TYPE
ErrorDocument 415 /index.php?page=error&tipo=12
#HTTP_INTERNAL_SERVER_ERROR
ErrorDocument 500 /index.php?page=error&tipo=13
#HTTP_NOT_IMPLEMENTED
ErrorDocument 501 /index.php?page=error&tipo=14
#HTTP_BAD_GATEWAY
ErrorDocument 502 /index.php?page=error&tipo=15
#HTTP_SERVICE_UNAVAILABLE
ErrorDocument 503 /index.php?page=error&tipo=16
#HTTP_VARIANT_ALSO_VARIES
ErrorDocument 506 /index.php?page=error&tipo=17
# MORE RULES FOR WEB PAGES