如何维护2个单独的.htaccess文件,以避免在主.htaccess文件中转储规则。
例如:我想在^ shows(/.*)?$之后跟踪所有的url指向在控制器/ show(.htaccess)下创建的.htaccess
答案 0 :(得分:1)
根据您的重写规则,这可以通过.htaccess
文件的最小重写(heh)来完成。例如,存储在webroot .htaccess
中的/var/www/html
文件:
RewriteEngine On
RewriteRule ^/foo/help/(.*)? /foo/help.php?q=$1
Rewriterule ^blog/(.*)? blog/index.php?id=$1
我们在这里做两种重写:
www.example.com/foo/help/123 -> wwww.example.com/foo/help.php?q=123
www.example.com/blog/myarticle -> www.example.com/blog/index.php?id=myarticle
注意第一个RewriteRule
使用绝对路径,而第二个使用相对路径。
如果我要将第一个RewriteRule
移到/var/www/html/foo/.htaccess
,该文件将如下所示:
RewriteEngine On
RewriteRule ^/foo/help/(.*)? /foo/help.php?q=$1
请注意,由于您使用的是绝对路径,因此无需更改任何内容。
现在,如果我将第二个RewriteRule
移到/var/www/html/blog/.htaccess
,该文件将如下所示:
RewriteEngine On
RewriteBase /blog/
RewriteRule ^(.*)? index.php?id=$1
请注意RewriteRule
中的路径被剥离,使它们相对于当前目录(现在为/blog
)。另请注意RewriteBase
指令,这在子目录中使用相对重写规则时是必需的。
查看this question以了解使用子目录.htaccess
重写规则的另一个示例。