如何使用mod_rewrite重写两个子文件夹的规则

时间:2013-02-07 17:57:50

标签: .htaccess mod-rewrite isapi isapi-rewrite isapi-redirect

我需要做这样的事情:

########## SITE 1 
RewriteEngine on 
RewriteBase /mysite1  
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA] 

########## SITE 2
RewriteEngine on 
RewriteBase /mySecondSite   
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA] 

问题是我只能使用一个.htaccess文件,因为我在Windows 2003 Server上使用Helicon ISAPI_Rewrite 3

有没有办法将两个.htaccess文件合并到其中一个文件中并使它们正常工作?

我试过这只是为了测试mysite是否可以在没有RewriteBase的情况下工作,但似乎不起作用:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^mysite1/(.*)$ index.php?url=$1 [L,QSA] 

感谢。

3 个答案:

答案 0 :(得分:1)

我无法想出比这更好的东西:

########## SITE 1 
RewriteEngine on 
RewriteCond %{HTTP:Host} ^www\.website\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /mysite1/index.php?url=$1 [L,QSA] 

########## SITE 2
RewriteEngine on 
RewriteCond %{HTTP:Host} ^www\.demo\.website\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /mySecondSite/index.php?url=$1 [L,QSA] 

答案 1 :(得分:0)

如果您的文件夹名称与网站名称相同:

RewriteEngine on 

RewriteCond %{HTTP:Host} ^www\.([^.]+)\.com$
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /%1/index.php?url=$1 [L,QSA] 

答案 2 :(得分:0)

最后我这样做了:

RewriteEngine on 
RewriteBase /

########## SITE 1 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^site1/(.*)$ site1/index.php?url=$1 [L, QSA]

########## SITE 2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^site2/(.*)$ site2/index.php?url=$1 [L, QSA]

########## SITE 3 (by URL) 
RewriteCond %{HTTP_HOST} ^mysite3.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.mysite3.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?url=$1 [L, QSA]