重写htaccess所以它不需要重复

时间:2015-01-20 17:57:28

标签: apache .htaccess mod-rewrite rewrite

我有一个大的htaccess文件可以完成网站的所有重定向。以下是我为每个目录所做的事情:

RewriteRule ^CAKE_MIX/.php$  CAKE_MIX/index.php  [R,L]
RewriteRule ^CAKE_MIX$  CAKE_MIX/  [R,L]
RewriteRule CAKE_MIX/faqs.php$ /faqs/faqs.php?cat=CAKE_MIX [QSA]
RewriteRule CAKE_MIX/index.php$ /overview/overview.php?cat=CAKE_MIX [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /CAKE_MIX/*
RewriteRule . /faqs/faqs.php?cat=CAKE_MIX&question=%{REQUEST_URI}

RewriteRule ^COOKIE_DOUGH/.php$  COOKIE_DOUGH/index.php  [R,L]
RewriteRule ^COOKIE_DOUGH$  COOKIE_DOUGH/  [R,L]
RewriteRule COOKIE_DOUGH/faqs.php$ /faqs/faqs.php?cat=COOKIE_DOUGH [QSA]
RewriteRule COOKIE_DOUGH/index.php$ /overview/overview.php?cat=COOKIE_DOUGH [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /COOKIE_DOUGH/*
RewriteRule . /faqs/faqs.php?cat=COOKIE_DOUGH&question=%{REQUEST_URI}

我为18个不同的目录执行此操作。我如何重写htaccess来说“对于以下任何目录:COOKIE_DOUGH,CAKE_MIX,ICE_CREAM,遵循这些规则?”然后规则可能是:

RewriteRule ^DIRECTORY/.php$  DIRECTORY/index.php  [R,L]

只需要写一次?这可能吗?

1 个答案:

答案 0 :(得分:1)

您可以使用正则表达式替换:

RewriteRule ^(CAKE_MIX|COOKIE_DOUGH|ICE_CREAM)/\.php$ $1/index.php  [R,L]

RewriteRule ^(CAKE_MIX|COOKIE_DOUGH|ICE_CREAM)$ $1/  [R,L]
RewriteRule (CAKE_MIX|COOKIE_DOUGH|ICE_CREAM)/faqs\.php$ /faqs/faqs.php?cat=$1 [QSA]
RewriteRule (CAKE_MIX|COOKIE_DOUGH|ICE_CREAM)/index\.php$ /overview/overview.php?cat=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(CAKE_MIX|COOKIE_DOUGH|ICE_CREAM)/
RewriteRule . /faqs/faqs.php?cat=%1&question=%{REQUEST_URI}