我怎样才能捕捉与mod-rewrite完全合格的本地URL路径? [英] How can I capture a fully qualified local URL path with mod rewrite?

查看:93
本文介绍了我怎样才能捕捉与mod-rewrite完全合格的本地URL路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能捕捉与mod-rewrite一个完全合格的本地URL路径?

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

例如,我的项目位于的http:// {本地主机} /projects/mywebsite/index.php

我试着用这个的RewriteBase /项目/ mywebsite / ,但它并不适用于的ErrorDocument 工作

I tried with this RewriteBase /projects/mywebsite/ but it does not work for ErrorDocument.

我的.htaccess,

my .htaccess,

RewriteEngine on

RewriteBase /projects/mywebsite/

ErrorDocument 404 /error.php

我需要做的的ErrorDocument 404 /projects/mywebsite/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.

任何想法?

推荐答案

的ErrorDocument 不接受不同于的mod_rewrite 规则。这里是你可以做的:

ErrorDocument doesn't accept any env variable unlike mod_rewrite rules. Here is what you can do:

  1. 而不是的ErrorDocument 使用的mod_rewrite 规则来处理404问题(见下文)
  2. 生成的RewriteBase 值动态地使用规则
  1. Instead of ErrorDocument use mod_rewrite rules to handle 404 issues (see below)
  2. Generate RewriteBase value dynamically using rules

您可以在您的根目录的.htaccess使用code:

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]

这篇关于我怎样才能捕捉与mod-rewrite完全合格的本地URL路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆