未知目录的RewriteRule [英] RewriteRule for unknown directory
问题描述
因此,我试图获得一个mod_rewrite规则,以将请求重定向到带有.htaccess文件的php脚本.问题是,无论我将项目放在网络服务器上的什么位置,我都希望它能工作(.htaccess文件和php-script始终位于同一文件夹中).
So I'm trying to get a mod_rewrite rule to redirect requests to a php-script with an .htaccess file. The thing is, I want it to work regardless of where I put the project on a webserver (the .htaccess file and the php-script are always in the same folder).
重写本身非常简单.如果脚本和.htacess位于目录/path/to/project
中,并且用户访问:
The rewrite itself is very simple. If the script and the .htacess are in the directory /path/to/project
and the user visits:
/path/to/project/somestring
应将其重写为:
/path/to/project/index.php?t=somestring
这应该适用于Web服务器中任何级别的每个子目录.所以:
This should work for every subdirectory at any level in the webserver. So:
如果php-script和.htaccess文件位于根目录中:
If the php-script and the .htaccess files are in the root:
/somestring2
应重写为:
/index.php?t=somestring2
如果php脚本和.htaccess文件位于/子目录
中:
/subdirectory/somestring3
应重写为:
/subdirectory/index.php?t=somestring3
因此,无论项目在服务器中的何处,RewriteRule都应执行相同的重写操作.要成为GET参数的字符串可以包含以下字符: [a-zA-Z0-9]
.如果请求的URL中还有其他GET参数,则也应附加它们(因此有QSA标志).这是我尝试过的:
So the RewriteRule should perform the same rewrite action regardless of where the project lives within the server. The string that is to become a GET-parameter can consist of those characters: [a-zA-Z0-9]
. If there are other GET-parameters in the requested URL, they should be appended as well (hence the QSA flag). This is what I've tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*/)([a-zA-Z0-9])/? $1index.php&t=$2 [L,QSA]
但是,这会导致404错误.我怎样才能改变它来做我想做的事?
However, this results in a 404 error. How can I alter it to do what I want?
推荐答案
尝试:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*/)?([A-Za-z0-9]+)/?$ /$1index.php?t=$2 [NC,L,QSA]
请注意,在 RewriteRule
上下文中,不需要在重写模式中使用斜杠.
Note that a leading slash in rewrite pattern is not required in the RewriteRule
context.
这篇关于未知目录的RewriteRule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!