.htaccess重定向与子域

时间:2017-02-03 09:21:41

标签: apache .htaccess redirect

我有很多网站可以通过以下子域访问:

website1.com.banana.servers.com
website2.net.apple.servers.com
website3.com.avocado.servers.com

我也相应地为每一个域提供域名:

website1.com
website2.net
website3.com

我需要做的就是编写一个反转的.htaccess规则,将子域名(以randomWord.servers.com结尾)重定向到一个短域。

例如:website1.com.banana.servers.com应该变为 - > website1.com

2 个答案:

答案 0 :(得分:2)

尝试这样我现在没试过,

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\/]*)\.([^\/]*)\.([^\/]*)\.([^\/]*)\.com
RewriteRule ^ http://%1.com [R=301,L]

答案 1 :(得分:1)

#The line above tells the server to turn on the engine for rewriting urls.    
RewriteEngine On
    #If the host is "sub.domain.com"
     RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC]
     #Then rewrite any request to /folder
#This line is a condition for the RewriteRule where We match against the http Host using regex pattern. The condition says that if the host is sub.domain.com then execute the Rule.
     RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]