简体   繁体   中英

How do I redirect to different page depending on the HOSTNAME .htacces with a Rewrite Condition/Rule

I have a site: www.asdf.com and an alias of www.fdsa.com

How can I check the HOSTNAME in the .htaccess and redirect them to different pages?

I know it's something along the lines of this:

RewriteRule (%{HTTP_HOST})^(.*)$ http://www.gunslot.com/$2 [L,R=301]

Basically how do I do an equivalent of this in the .htaccess (pseudo code)

if(hostname == "asdf") redirect to asdf.com/hello.html
if(hostname == "fdsa") redirect to asdf.com/goodbye.html

Not 100% clear what your trying to achieve but this answer's what you have asked.

.htaccess

RewriteCond %{HTTP_HOST} ^asdf\.com
RewriteRule ^(.*)$ http://asdf.com/hello.html [NC,L]

RewriteCond %{HTTP_HOST} ^fdsa\.com
RewriteRule ^(.*)$ http://asdf.com/goodbye.htm [NC,L]

If you want to redirect any request to asdf domain to hello.html on asdf, and any request to fdsa to goodbye.html add the following to your .htaccess file in the root of your domain.

RewriteEngine On
RewriteBase /
#Redirect any asdf domain or subdomain
RewriteCond %{HTTP_HOST} ^(.+\.)?asdf\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/(hello|goodbye)\.html$ [NC]
RewriteRule . http://asdf.com/hello.html [L,R]

#Redirect any fdsa domain or subdomain
RewriteCond %{HTTP_HOST} ^(.+\.)?fdsa\.com$ [NC]
RewriteRule . http://asdf.com/goodbye.htm [L,R]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM