混淆.htaccess子域重写(不重定向)

时间:2012-06-06 05:45:44

标签: regex .htaccess url subdomain masking

我有两个指向我网站根目录的域名:

http://site1.com
http://site2.com

both point to http://dXXXXXXX.domain.com/

我已将每个站点设置为在子目录(同名)中查找其文件,所以

http://site1.com files are located in http://dXXXXXXX.domain.com/site1/
http://site2.com files are located in http://dXXXXXXX.domain.com/site2/

我想为site1(可能还有site2)创建一个特殊子域,它实际指向主目录中的文件夹(两个域都可以共享的论坛)。我想用子域名url掩盖真实的URL,如下所示:

http://subdomain.site1.com masks http://dXXXXXXX.domain.com/shared_folder/ (not redirected)

我的.htaccess文件(在最顶层的目录中)如下所示:

Options +FollowSymLinks
RewriteEngine On

# Rewrite "www.domain.com -> domain.com" 
<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>

# redirect site1.com to /site1 [folder]
# redirect site2.com to /site2 [folder]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^0-9]+)$ [NC]
RewriteRule ^(.*)$ /%1/$1 [QSA,L]

#i think this should work but it doesn't
#RewriteCond %{HTTP_HOST} ^subdomain\.site1\.com$ [NC]
#RewriteRule ^$ shared_folder [P]

另外,如果可能的话,我想将每个域的绝对路径重定向到我的首选路径,如下所示:

http://dXXXXXXX.domain.com/site1/ redirects to http://site1.com
http://dXXXXXXX.domain.com/site2/ redirects to http://site2.com

不确定是否有人会找到这些链接,但我希望通过不为同一段内容提供多个访问点来保持一切清洁(除了故意共享的css或论坛软件)

提前感谢您对我的任何帮助或见解

1 个答案:

答案 0 :(得分:0)

我想我已经明白了(因为它尚未破裂!)

Options +FollowSymLinks
RewriteEngine On

# Rewrite/Redirect "www.domain.com -> domain.com" 
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# Rewrite/Redirect "domain.com/foo -> domain.com/foo/"
RewriteCond %{REQUEST_FILENAME} !-f         
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$   
RewriteRule ^(.*)$ /$1/ [R=301,L]       

# Redirect/Pass through "subdomain.site1.com to /shared_folder/"
RewriteCond %{HTTP_HOST} ^subdomain\.([\w.]+)$ [NC]
RewriteRule ^(.*)$ /shared_folder/ [L]

# Redirect/Pass through site1.com to /site1 [folder]
# Redirect/Pass through site2.com to /site2 [folder]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^0-9]+)$ [NC]
RewriteRule ^(.*)$ /%1/$1 [QSA,L]

所以这有效,但我不确定我的规则的位置:如果最好在域名本身之前处理特殊的子域名,就像我现在的顺序一样;或者如果RewriteRule ^(.*)$ /shared_folder/ [L]更好地写为RewriteRule ^$ /shared_folder/ [L] ...

如果您对好的或更好的做法有任何见解,我将不胜感激,谢谢。