问题
How can I capture a fully qualified local URL path with mod rewrite?
For instance, my project is located in http://{localhost}/projects/mywebsite/index.php
I tried with this RewriteBase /projects/mywebsite/ but it does not work for ErrorDocument.
my .htaccess,
RewriteEngine on
RewriteBase /projects/mywebsite/
ErrorDocument 404 /error.php
I need to do ErrorDocument 404 /projects/mywebsite/error.php but it isn't dynamic. I have to change it in two places everytime for each project.
Any ideas?
回答1:
ErrorDocument doesn't accept any env variable unlike mod_rewrite rules. Here is what you can do:
- Instead of
ErrorDocumentusemod_rewriterules to handle 404 issues (see below) - Generate
RewriteBasevalue dynamically using rules
You can use this code in your root .htaccess:
RewriteEngine On
RewriteBase /
# Determine the RewriteBase automatically/dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
# use %{ENV:BASE} variable set above for routing file/dir not found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . %{ENV:BASE}error.php?e=404 [L,QSA]
回答2:
The ErrorDocument is part of the apache core, it isn't affected by the Rewrite stuff which is a completely separate module.
If you need to output the contents of error.php, then output it from the php header() (based on your previous question) and don't rely on ErrorModule. Maybe something like:
header("HTTP/10 404 Not Found");
include("error.php");
来源:https://stackoverflow.com/questions/27056404/how-can-i-capture-a-fully-qualified-local-url-path-with-mod-rewrite