简体   繁体   English

HTACCESS重写不适用于非WP目录

[英]HTACCESS rewrite not working for non WP directory

I have WordPress installed in the root folder of my domain example.com and have another directory which has custom PHP code example.com/control/ . 我有WordPress安装在我的域example.com的根文件夹中,并有另一个目录,其中包含自定义PHP代码example.com/control/

With no WordPress installation my .htaccess file was redirecting /control/show.php?id=2 as /control/2/ and working perfectly fine but once I have installed WP in root it's no longer working. 没有WordPress安装我的.htaccess文件重定向/control/show.php?id=2作为/control/2/并且工作得非常好但是一旦我在root中安装了WP它就不再工作了。

ROOT .htaccess : 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

/control/.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^control/([^/]*)$ /control/show.php?id=$1 [L,QSA,NC]

Please, help me understand the error and how can I make it work. 请帮助我理解错误以及如何使其工作。

control/.htaccess

 RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^control/([^/]*)$ /control/show.php?id=$1 [L,QSA,NC] 

This doesn't work in the /control/.htaccess file because the RewriteRule directive is written as if it is in the .htaccess file in the document root. 这在/control/.htaccess文件中不起作用,因为RewriteRule指令的编写就好像它位于文档根目录中的.htaccess文件中一样。

In .htaccess the RewriteRule pattern matches against the URL-path less the directory-prefix (that lead to the .htaccess file). .htaccessRewriteRule 模式与URL路径匹配,而不是目录前缀(导致.htaccess文件)。 In other words, it matches against 2/ , not control/2/ . 换句话说,它匹配2/ ,而不是control/2/ That is the reason why the above directive is not working. 这就是为什么上述指令不起作用的原因。

Not an error, but you don't need to explicitly state the /control/ subdirectory in the substitution either, as this will be prefixed automatically, providing you use a relative path substitution. 不是错误,但您不需要在替换中显式声明/control/子目录,因为这将自动添加前缀,前提是您使用相对路径替换。 Likewise, you don't need the RewriteBase directive either (it's arguably set incorrectly anyway in this context). 同样,你也不需要RewriteBase指令(在这种情况下,它可能被设置为错误)。

So, the above should be written like the following instead: 所以,上面应该写成如下:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ show.php?id=$1 [QSA,L]

The NC flag is also superfluous in this example. 在这个例子中, NC标志也是多余的。

You should be as specific as possible in your regex. 你应该在你的正则表达式中尽可能具体。 If the "id" is always digits (as in your example) then modify the RewriteRule pattern to match only digits: ^(\\d*)$ (0 or more digits). 如果“id”始终为数字 (如示例所示),则修改RewriteRule 模式以仅匹配数字: ^(\\d*)$ (0或更多位数)。

You don't need to create a control .htaccess 您不需要创建控件.htaccess

RewriteRule ^control/([^/]*)$ /control/show.php?id=$1 [L,QSA,NC]

Add the above code before # BEGIN WordPress in the wordpress htaccess file. 在wordpress htaccess文件中的#BEGIN WordPress之前添加上面的代码。

Hope this might help. 希望这可能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM