Jeramiah Harland
Jeramiah Harland

Reputation: 864

.htaccess redirect domain alias to specific page

The company I work for is opening three restaurants, one has a different name than the others. The site is the same except logos and content. For this reason I've set up a domain alias and it all works fine.

However, they'd like me to redirect domain2.com so that, instead of the domain1.com homepage, people are sent to domain2.com/example1/

Here is what I have so far:

RewriteCond %{HTTP_HOST} ^www\.domain2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/example1/?$
RewriteRule ^(.*)$ http://domain2.com/example1/ [R=302,L]

RewriteCond %{HTTP_HOST} ^www\.domain1\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/example1/?$
RewriteRule ^(.*)$ http://domain2.com/example1/ [R=302,L]

This works... sort of.

BUT! Not entirely...

Examples:

When HTTP_HOST is 'http://domain2.com' this works

When HTTP_HOST is 'http://www.domain2.com'

Upvotes: 0

Views: 2661

Answers (1)

Jeramiah Harland
Jeramiah Harland

Reputation: 864

I've finally got it working.

I've updated my htaccess to look like this:

RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://domain2.com/example1/ [R=302,L]

So the trick ended up being:

RewriteCond %{REQUEST_URI} ^/$

I read that %{REQUEST_URI} always contains a leading slash, it's never empty, so by checking if it only contains that I was able to straighten out my redirects.

Now, if HTTP_HOST is www.domain2.com

  • RewriteCond %{HTTP_HOST} ^(www.)?domain2.com$ [NC] = TRUE
  • RewriteCond %{REQUEST_URI} ^/$ = TRUE
  • Redirect!

And, if HTTP_HOST is www.domain2.com/example1/

  • RewriteCond %{HTTP_HOST} ^(www.)?domain2.com$ [NC] = TRUE
  • RewriteCond %{REQUEST_URI} ^/$ = FALSE
  • No Redirect! Woo!

Upvotes: 3

Related Questions