Regex .htaccess,分别重定向到自己的index.php
我为一个看似重复的问题道歉,但我所看到的几十个问题中没有一个是相同的问题 我有以下目录结构:Regex .htaccess,分别重定向到自己的index.php,regex,apache,.htaccess,mod-rewrite,subdirectory,Regex,Apache,.htaccess,Mod Rewrite,Subdirectory,我为一个看似重复的问题道歉,但我所看到的几十个问题中没有一个是相同的问题 我有以下目录结构: /.htaccess /index.php /subfolder/.htaccess /subfolder/index.php 我希望所有页面请求都由/index.php处理,除非请求启动/subfolder,在这种情况下,它应该由/subfolder/index.php处理 e、 g./abc重写为/index.php?u=abc e、 g./subfolder/def要重写为/subfolder
/.htaccess
/index.php
/subfolder/.htaccess
/subfolder/index.php
我希望所有页面请求都由/index.php
处理,除非请求启动/subfolder
,在这种情况下,它应该由/subfolder/index.php
处理
- e、 g.
重写为/abc
/index.php?u=abc
- e、 g.
要重写为/subfolder/def
/subfolder/index.php?u=def
index.php
处理,而不是子文件夹。(对/子文件夹的请求除外)
当前文件内容
/.htaccess
Options -Indexes -MultiViews +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^(.*)$ /index.php?u=$1 [NC,QSA]
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /admin/index.php?u=$1 [NC,QSA]
/subfolder/.htaccess
Options -Indexes -MultiViews +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^(.*)$ /index.php?u=$1 [NC,QSA]
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /admin/index.php?u=$1 [NC,QSA]
让您的root.htaccess如下所示:
Options -Indexes -MultiViews +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!admin/)(.+)$ /index.php?u=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin/(.+)$ /admin/index.php?u=$1 [NC,QSA,L]
对于这个简单的要求,不需要在admin文件夹中有.htaccess。根文件夹的这一行。htaccess:
RewriteRule ^(.*)$ /index.php?u=$1 [NC,QSA]
正在导致对不存在的文件路径的所有请求重定向到根文件夹的index.php
。这就是问题所在。
一种可能的解决方案是用这两条线替换上述线:
RewriteRule ^subfolder/(.*)$ /subfolder/index.php?u=$1 [L,NC,QSA]
RewriteRule ^(.+)$ /index.php?u=$1 [L,NC,QSA]
通过添加L
(last)标志并按此顺序编写规则,您将使Apache正确重定向您的请求,并消除将指令重写到/subfolder/.htaccess
中的需要。我曾经尝试过类似的方法。这不就是重定向吗?@jezmck不,这是由[R]标志完成的。[五十] 导致从进一步的规则应用程序停止重写。解释了我的意思。@anubhava如果WordPress是主站点,而CodeIgniter位于子文件夹中,那么这不起作用,原因可能是什么。请参见此处,大多数mod_rewrite
规则不适用于具有前端控制器架构(如WP、Joomla、CI等)的PHP框架。最好使用框架提供的API来重写内容,而不是mod_rewrite
。
Options -Indexes -MultiViews +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^(.*)$ /index.php?u=$1 [NC,QSA]
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /admin/index.php?u=$1 [NC,QSA]