Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess to redirect 2 different domain names to seperate "landing pages"

Upon request I have reformulated my question for clarity so others might benefit better from the answers.

I have 3 domains for the company I work for:

bizwizprint.com (the main website that is hosted on a shared server)

bizwizsigns.com (secondary domain with no hosting attached)

boatwiz.com (tertiary domain with no hosting attached)

The goal is to get my second and third domains to redirect to the first domain onto their own respective landing pages.

First Step: At the domain registrar, change the DNS "A Records" of the second and third domains to resolve to the same IP address that the main website is hosted on.

Second Step: Create a "Site Alias" on the main website server for the second and third domains, they will point to the root directory where the main website files reside.

Third Step: Create or edit an .htaccess file that will redirect the requests for the second and third domains and point them to the landing pages that I have created for them.

The question: What rules do I add to htaccess?

Essentially, I would like to have a user type in "boatwiz.com" in the address bar and the browser will literally GO TO "bizwizprint.com/boatwiz.html".

Please note: I do not want any rewrite rules that will change the actual URL to boatwiz.

The reason for this is that it is a temporary thing. Eventually there will be an actual "boatwiz" website and "bizwizsigns" website and they will most likely be very different in structure. I don't want it to appear that I have three domains with all the same content, or have people make any bookmarks that I will need to redirect yet again in the future.

like image 547
Grapho Avatar asked Dec 30 '13 22:12

Grapho


People also ask

Does .htaccess do redirect?

Use a 301 redirect . htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain.

Can you point 2 domains to the same site?

Pointing two URLs to the same website is a good way to direct traffic to your site from several different domain names. You can accomplish this in two ways: either redirect one of the URLs to your primary domain, or create an alias for one of the URLs. The alias would point that domain towards your primary domain.


2 Answers

"How do I redirect an external domain (boatwiz.com) to land in a specific page of a new domain (bizwizprint.com/boatwiz.html) without any rewriting?"

So you probably mean that you want an "internal redirect", not the "external redirect", right? I.e. you want e.g. the bizwizsigns.com to stay displayed in the browser Location bar, but show the contents of bizwizprint.com/signs, right?

1. htaccess only - impossible

Well, using only .htaccess, this is impossible, because the different domain will force the external redirect. Citing the docs:

Absolute URL

If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.

2. Iframe trick

What you could do is to use iframe. Put this code at bizwizsigns.com/index.html:

<iframe src="http://bizwizprint.com/signs" width="100%" height="100%" 
        style="border: 0 none;" frameborder="0">

But there are many downsides of this solution:

  • the URL will not change in the browser's location bar, even if user clicks within the iframe
  • browser bookmarks and history will not work as expected.

3. Clever hosting setup (domain aliases into the same dir)

Are you in a hosting environment, or do you have your own server? Sometimes the hosting allows you to make aliases of several domains that are handled by the same local directory tree. In that case, you won - you can write .htaccess so that it handles the requests as internal redirects:

RewriteCond %{HTTP_HOST} bizwizsigns\.com$ [NC]
RewriteRule ^$ /signs [L]

RewriteCond %{HTTP_HOST} boatwiz\.com$ [NC]
RewriteRule ^$ /boats [L]

which will (internally) redirect bizwizsigns.com to /signs (= your content of bizwizprint.com/signs, because you have one hosting server directory for all 3 domains). But if you e.g. want all queries like bizwizsigns.com/<foo> to be redirected to bizwizprint.com/signs/<foo>, you have to be more careful - see the added condition on REQUEST_URI to prevent endless loop:

RewriteCond %{HTTP_HOST} bizwizsigns\.com$ [NC]
RewriteCond %{REQUEST_URI}  !^/signs/ [NC]
RewriteRule ^.*$ /signs/$1 [L]
like image 91
Tomas Avatar answered Dec 06 '22 04:12

Tomas


Assuming that you have all 3 domains pointing to the same document root, you just need this in its htaccess file:

RewriteCond %{HTTP_HOST} bizwizsigns\.com$ [NC]
RewriteRule ^$ http://bizwizprint.com/signs [L,R=301]

RewriteCond %{HTTP_HOST} boatwiz\.com$ [NC]
RewriteRule ^$ http://bizwizprint.com/boats [L,R=301]

RewriteCond %{HTTP_HOST} (bizwizsigns|boatwiz)\.com$ [NC]
RewriteRule ^(.+)$ http://bizwizprint.com/$1 [L,R=301]

So if it's just http://bizwizsigns.com/ or http://boatwiz.com/, then you get redirected to http://bizwizprint.com/signs or http://bizwizprint.com/boats. But if you have anything after the last /, like http://bizwizsigns.com/foo/bar.html then you'll get redirected to http://bizwizprint.com/foo/bar.html.

like image 41
Jon Lin Avatar answered Dec 06 '22 05:12

Jon Lin