Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.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/1/oracle/10.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
Php HTACCESS重写不适用于非WP目录_Php_Wordpress_.htaccess_Mod Rewrite - Fatal编程技术网

Php HTACCESS重写不适用于非WP目录

Php HTACCESS重写不适用于非WP目录,php,wordpress,.htaccess,mod-rewrite,Php,Wordpress,.htaccess,Mod Rewrite,我在我的域example.com的根文件夹中安装了WordPress,并且还有另一个目录,其中包含自定义PHP代码example.com/control/ 在没有WordPress安装的情况下,我的.htaccess文件将/control/show.php?id=2重定向为/control/2/,工作非常正常,但一旦我在root中安装了WP,它就不再工作了 ROOT.htaccess: # BEGIN WordPress <IfModule mod_rewrite.c> Rewrit

我在我的域
example.com
的根文件夹中安装了WordPress,并且还有另一个目录,其中包含自定义PHP代码
example.com/control/

在没有WordPress安装的情况下,我的
.htaccess
文件将
/control/show.php?id=2
重定向为
/control/2/
,工作非常正常,但一旦我在root中安装了WP,它就不再工作了

ROOT
.htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
# END WordPress
请帮助我了解错误以及如何使其工作。

您不需要创建控件。htaccess

在WordPress htaccess文件的#BEGIN WordPress之前添加上述代码

希望这能有所帮助

control/.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^control/([^/]*)$ /control/show.php?id=$1 [L,QSA,NC]
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^control/([^/]*)$ /control/show.php?id=$1 [L,QSA,NC]
这在
/control/.htaccess
文件中不起作用,因为
RewriteRule
指令的编写方式与文档根目录中的
.htaccess
文件类似

.htaccess
中,
重写规则
模式匹配URL路径减去目录前缀(导致
.htaccess
文件)。换句话说,它匹配
2/
,而不是
control/2/
。这就是上述指令不起作用的原因

这不是一个错误,但是您也不需要在替换中显式地声明
/control/
子目录,因为如果使用相对路径替换,它将自动作为前缀。同样,您也不需要
RewriteBase
指令(在本上下文中,它的设置可能不正确)

因此,上述内容应改为如下所示:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ show.php?id=$1 [QSA,L]
在本例中,
NC
标志也是多余的

您应该在正则表达式中尽可能具体。如果“id”始终是数字(如您的示例中所示),则修改
重写规则
模式以仅匹配数字:
^(\d*)$

(0位或更多位)。

RewriteBase/
@Phil谢谢你的回复,你能告诉我RewriteBase/有什么问题吗?这需要在根文件和子目录文件夹htaccess中更正吗?在我看来,很少需要使用
RewriteBase
@Phil你建议从这两个文件夹中删除并尝试一下吗?已删除,仍然无法工作:(虽然在
/control
子目录中不一定需要一个单独的
.htaccess
文件,但可能仍然需要将其与WordPress配置完全分开。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ show.php?id=$1 [QSA,L]