Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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
Regex .htaccess,分别重定向到自己的index.php_Regex_Apache_.htaccess_Mod Rewrite_Subdirectory - Fatal编程技术网

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]