让.htaccess RewriteRule重定向到“当前目录"中的脚本. (而不是明确的路径) [英] Let .htaccess RewriteRule redirect to a script "in current dir" (instead of an explicit path)

查看:180
本文介绍了让.htaccess RewriteRule重定向到“当前目录"中的脚本. (而不是明确的路径)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.htaccess中使用RewriteRule将不是现有文件的任何内容重定向到动态处理任何请求的"cms.php"文件(或在适当时输出一些错误消息) ).

I'm using a RewriteRule in .htaccess to redirect anything that is not an existing file, to a "cms.php" file which dynamically handles any request (or outputs some error message if appropriate).

这是我在.htaccess中所做的:

Here's what I do in .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /cms.php [L,QSA]

像魅力一样工作.

但是,出于开发目的,我也希望在本地XAMPP系统上托管相同的站点.在我实际的实时网络服务器上,cms.php就在文档根文件夹中,因此我可以在其中使用/cms.php.但是在我的本地开发机器上,该站点位于某个子目录中,即/cms.php不是正确的路径.

However, for development purposes, I want to host the same site on my local XAMPP system as well. On my actual live webserver, the cms.php is right in de document root folder, hence I can use /cms.php there. But on my local development machine, this site is in some subdir, i.e. /cms.php is not the right path.

我也尝试了"cms.php"(不带/),也尝试了"./cms.php"(希望."表示当前目录),但无济于事.只有当我在RewriteRule中明确指定/the/correct/subdir/cms.php时,它才能正常工作,但是显然,这仅在开发计算机上有效,而在我的实时Web服务器上无效.

I also tried "cms.php" (without the / in front of it) as well as "./cms.php" (hoping that "." would denote the current dir) but to no avail. Only when I explicitly specify /the/correct/subdir/cms.php in the RewriteRule, it works OK, but obviously this is only valid on the development machine and not on my live webserver.

我可以以某种方式使用智能路径或mod_rewrite变量之类的东西,以便.htaccess理解它需要重定向到与.htaccess本身驻留的目录相同的目录中的我的cms.php文件 ?

Can I somehow use a smart path or mod_rewrite variable or something, so that .htaccess understands it needs to redirect to my cms.php file in the same dir as where .htaccess itself resides ?

推荐答案

通常,您只需在dev目录中设置RewriteBase,并在规则目标的斜杠前留一个斜杠:

Typically, you'd just set a RewriteBase to the dev directory and leave the leading slash off of your rule's target:

RewriteEngine On
RewriteBase /subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ cms.php [L,QSA]

但是,如果这还不够好,并且您希望能够随意移动htaccess文件而不必始终更改基准,那么您可以通过检测任意基准来尝试做一些疯狂的事情:

But if that's not good enough, and you want to be able to move the htaccess file around arbitrarily without having to alter the base all the time, ou can try to do something crazy by detecting an arbitrary base:

RewriteCond %{ENV:URI} ^$
RewriteRule ^(.*)$ - [ENV=URI:$1]

RewriteCond %{ENV:BASE} ^$
RewriteCond %{ENV:URI}::%{REQUEST_URI} ^(.*)::(.*?)\1$
RewriteRule ^ - [ENV=BASE:%2]

位于htaccess文件的最顶部,然后使用BASE环境变量:

at the very top of your htaccess file, then using the BASE environtment variable:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}cms.php [L,QSA]

这篇关于让.htaccess RewriteRule重定向到“当前目录"中的脚本. (而不是明确的路径)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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