设置
我的设置是为两个或多个具有相似结构但内容不同的站点共享资源。例如...
http://localhost/site1/
http://localhost/site2/
有两种类型的重写,CMS 内容(几乎只是回显到页面的内容)和特殊模块(例如博客软件模块),其中我有软件可以更具体地处理来自数据库的内容。
所以博客的第一个重写规则确保博客模块处理博客请求......
http://localhost/site1/blog/*
http://localhost/site2/blog/*
...使用位于...的博客模块软件
http://本地主机/博客/
CMS 重写规则旨在处理非特定模块请求...
http://localhost/site1/*
http://localhost/site2/my_page.html*
...使用位于...的 CMS 重写软件
http://localhost/rewrite.php
问题
博客模块和 CMS 模块重写存在冲突。我尝试使用以下规则进行异常(exception)处理。这是我的代码...
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.js$
RewriteRule .*/blog(.+) blog$1 [QSA]
RewriteRule !.*/(admin|blog|forums)(.+)$ rewrite.php
最后一条规则实际上不起作用。使用此代码,如果我访问..
http://localhost/site1/blog/*
http://localhost/site2/blog/*
...任何网站的任何博客(或管理员或论坛)URL 仍在重写以与 localhost/rewrite.php 一起使用。
那么如何调整最后一条规则以满足以下条件...
1.) 第一个目录(localhost/site1/blog 中的 site1 或 site2)保持动态,因此我可以根据需要添加第三个站点,而无需出于任何原因重新调整代码。
2.) 博客(或管理员或论坛)索引(例如 blog/、forums/、admin/)由它们自己的模块以及这些目录中的任何内容(例如 admin/1、admin/test.html)处理无论 HTTP 代码、200、404 等如何。
3.) 任何不在最后一条规则异常(exception)列表中的 URL 都由 rewrite.php 处理(不管 HTTP 代码、200、404 等)。
4.) localhost/site1/blog/不被 rewrite.php 处理,并且 localhost/site1/random_path 不被博客模块重写处理。
我很乐意快速回复任何进一步的说明。
最佳答案
感谢之前的 RewriteCond,有人写的有意义,我对其进行了改编,效果很好!
请注意,如果有人决定使用此代码,则条件显然仅在模块(管理员、博客、论坛)的特定重写规则之后才有效,尽管在 CMS rewrite.php 规则之前。
我很乐意尊重任何积极的批评。
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|js|zip)$
RewriteRule .*/admin(.+) admin$1 [QSA]
RewriteRule .*/blog(.+) blog$1 [QSA]
RewriteRule .*/forums(.+) forums$1 [QSA]
#individual...
RewriteCond %{REQUEST_URI} !.*/admin
RewriteCond %{REQUEST_URI} !.*/blog
RewriteCond %{REQUEST_URI} !.*/forums
#condensed...
RewriteCond %{REQUEST_URI} !.*/(admin|blog|forums)
RewriteRule !\.(css|js|zip)$ rewrite.php
关于Apache 重写异常以不包含特定目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8310927/