Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess将子目录重定向到其index.php文件_Php_.htaccess_Url_Redirect - Fatal编程技术网

.htaccess将子目录重定向到其index.php文件

.htaccess将子目录重定向到其index.php文件,php,.htaccess,url,redirect,Php,.htaccess,Url,Redirect,我在这个问题上纠缠了很长时间。我会尽力解释这个问题,但如果没有解释清楚,请随时询问更多细节 我有两个目录,文档根目录和cms子目录。我试图做的是将每个没有子目录的URL(例如domain.com、domain.com/help)重定向到根目录的index.php,同时将包含cms子目录(例如-domain.com/cms/item)的每个URL重定向到cms目录的index.php。这是我的.htaccess文件,但遗憾的是它无法工作: Options -Indexes -MultiViews

我在这个问题上纠缠了很长时间。我会尽力解释这个问题,但如果没有解释清楚,请随时询问更多细节

我有两个目录,文档根目录和cms子目录。我试图做的是将每个没有子目录的URL(例如domain.com、domain.com/help)重定向到根目录的index.php,同时将包含cms子目录(例如-domain.com/cms/item)的每个URL重定向到cms目录的index.php。这是我的.htaccess文件,但遗憾的是它无法工作:

Options -Indexes -MultiViews +FollowSymLinks
RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!cms/)(.+)$ /index.php?u=$1 [NC,QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cms/(.+)$ /cms/index.php?u=$1 [NC,QSA,L]

任何帮助都将不胜感激,因为这是项目的关键部分,index.php文件处理搜索引擎优化并包含所需文件是必需的。

更改规则顺序,首先匹配
^cms/(.+)$
,第二个只使用
^(.+)$
,第二个使用
^([^/]+)$
,如果您想排除所有目录(即只允许不包含
/
的路径)@04FS,谢谢您的回复。不幸的是,当我试图访问cms子文件夹中的文件时,它仍然直接访问该文件,而不是访问/cms的index.php。你是说一个实际存在的文件?您从重写中排除的内容,使用重写条件,您正在检查请求的内容是否不是现有文件。@04FS是的,我的cms中URL的结构是“domain.com/cms/users.php”,而在公共部分类似于“domain.com/about”。其想法是在/cms的index.php中包含现有文件,同时在URL栏中保留URL。公共部门的工作完美无瑕。