如何在 htaccess 上重用条件块,这样我就不需要为新规则再次编写它


How to reuse condition blocks on htaccess so I don't need to write it again for a new rule?

我正在用Wordpress写博客,我有一个带有Codeigniter的网站,两者都在同一台服务器上。我正在为它们设置路由,我的.htaccess文件上有此规则

RewriteEngine on
RewriteCond $1 !^(images|robots'.txt|styles)
RewriteCond $1 !'.(ico|js|css|jpg|png|gif|html|ico?g)$
RewriteCond %{REQUEST_URI} blog/
RewriteRule ^(.*) blog/index.php [L]
RewriteCond $1 !^(index'.php|images|robots'.txt|styles)
RewriteCond $1 !'.(ico|js|css|jpg|png|gif|html|ico?g)$
RewriteRule ^(.*)$ index.php/$1 [L]

但请注意,大多数条件都是相同的,因为我两次重写都需要几乎相同的条件。

如果我删除第二个条件块并直接跳到第二个条件块RewriteRule则会出现服务器错误。是否可以对不同规则的条件进行分组(并最终添加一些额外的条件,就像我对第一条规则所做的那样)?

试一试:

RewriteEngine on
RewriteCond $1 !^(index'.php|images|robots'.txt|styles)
RewriteCond $1 !'.(ico|js|css|jpg|png|gif|html|ico?g)$
RewriteRule ^blog/.* blog/index.php [L]
RewriteRule ^ index.php [L]

我在捕获博客部分的.*时遇到了麻烦,因此未捕获。无论如何,不要认为它真的需要。