How can I capture a fully qualified local URL path with mod rewrite?

半城伤御伤魂 提交于 2020-01-16 04:21:31

问题


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:

  1. Instead of ErrorDocument use mod_rewrite rules to handle 404 issues (see below)
  2. Generate RewriteBase value 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!