homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Redirect domain to internal page, then rewrite

 12:31 pm on Feb 21, 2013 (gmt 0)


Need some help on this redirect.

I have a site called: www.a.co.uk and it contains a page called page1. www.a.co.uk/folder1/page1

I have other domains connected to the site

How can I access page1 typing the address www.b.com and have the browser url bar display www.a.co.uk/THISISPAGE1 if possible.




 12:43 pm on Feb 21, 2013 (gmt 0)

You'll need a RewriteRule coded as a 301 redirect.
You'll need a preceding condition that checks that HTTP_HOST was not exactly "www.a.co.uk".
This gets the user requesting the correct URL.

You'll need a RewriteRule coded as an internal rewrite.
This will connect the external URL request to the internal folder that will deliver the content, without revealing the name of that folder.


 1:12 pm on Feb 21, 2013 (gmt 0)


I have written

RewriteCond %{HTTP_HOST} ^www\.b\.com$ [NC]
RewriteRule http://www.a.co.uk/folder1/page1 http://www.a.co.uk/THISISPAGE1 [R=301,L]

Browser goes to [a.co.uk...] (which is great) but fails to display the content of [a.co.uk...]


 2:19 pm on Feb 21, 2013 (gmt 0)

Browser goes to http://www.example.co.uk/THISISPAGE1 (which is great) but fails to display the content of http://www.example.co.uk/folder1/page1

"I'm in London. Why don't I see the Eiffel tower when I look out the window?"

The browser, having arrived safely at page A, asks the server for the content living at that location. The server, having received no instructions to the contrary, provides the said content. Or reports that it can't find it, as the case may be.

:: wondering once again whether large numbers of people simply click Submit and then close the tab without even looking to see if their post has gone through as intended ::


 2:58 pm on Feb 21, 2013 (gmt 0)

okay.... Are you restating what I mentioned earlier because I do know it is not finding the content.
What I would need is a line of code, or some command to look into as I do not know how to tell htaccess which content to fetch.


 11:45 pm on Feb 21, 2013 (gmt 0)

What I would need is a line of code

Yes, that's right. One line.

As g1 said earlier, you need two elements: The redirect and the rewrite. You've got the redirect. Let's see how you do with the rewrite. In their simplest form, the two pieces will be identical except that the rewrite component has no Conditions and, of course, it goes in the opposite direction (target path and pattern path reversed).


 10:51 am on Feb 22, 2013 (gmt 0)

okay.. I give up. I tried a few things but no cigar. I'll go to another forum where members help solve problems and do not play riddle games.


 12:09 pm on Feb 22, 2013 (gmt 0)

Post what you tried, what it was supposed to do, what it actually did, how that differed from what you wanted, and perhaps someone can point out where you went wrong.

What we don't do here is write your code for you to cut and paste into your site with zero understanding of what that code does. We prefer to help you learn how it works, by helping "you" fix "your" code.


 2:33 pm on Feb 22, 2013 (gmt 0)

I have tried variations of the lines above and I dont' know what I am doing wrong. I am beginner at .htaccess condidering all you can do with it. Unfortunately, I have not the time to study it in details as I have 10 other spinning plates to deal with to make this website run. All I ask is a bit of help in terms of coding. Not the theory please because I know how it should work and it wil not help a bit.
I am obviously missing something fundamental but I have no clue as to what is is.


 2:44 pm on Feb 22, 2013 (gmt 0)

Without seeing your code, there's no way to point out the error.

There's 80 000 previous threads in this forum. Some 10 000 of them are for questions very similar to yours. Each time there is always some minor variation that makes the code in the answer very slightly different. Those other threads give a good starting point, but won't be exactly the right code.


 3:00 pm on Feb 22, 2013 (gmt 0)

Here it is

RewriteCond %{HTTP_HOST} ^www\.b\.com$ [NC]
RewriteRule [a.co.uk...] [a.co.uk...] [R=301,L]


 9:23 pm on Feb 22, 2013 (gmt 0)

If rewrites worked the way you are thinking, the above would be saying:

- if the user asks for b.com
- then rewite the url for a.co.uk to another address on a.co.uk.

This is like saying "If someone is driving down Hwy 5, create an offramp on Hwy 95 to detour them to go the other direction on Hwy 95". Since the driver on Hwy 5 isn't on Hwy 95, he's never going to see the offramp.

But actually, rewrites don't work the way you are thinking. The reason you need to test for the HTTP_HOST in the condition is because that is NOT available in the RewriteRule. So the domain in the match would have to be in the request for any match to ever occur. The replacement might (in a 301 redirect).

To actually answer your question, you still need to answer g1smd's original questions. What seems clear to you is in fact rather vague and you're not getting precise answers because you are not asking precise questions.

1. What does the original URL look like? Give three examples such as


As it stands from what you've said so far, you want to rewrite every request to b.com to the same page.

2. What URL do you want to show in the browser address bar for each one of these addresses?

www.b.com/folder/page1 -> a.co.uk/?
www.b.com/folder2/page1 -> a.co.uk/?
www.c.com/folder1/page1 -> a.co.uk/?

You say you want THISPAGE1 - will you just manually create a rewrite for every domain (which is fine) since there's no pattern here (i.e. no matching characters aside from the shared 1)

3. Where are the actual files you want these to point to? Based on what you have given us I assume this is custom in every case, but with only one example it's impossible to know what you're really asking

a.co.uk/THISPAGE1 --> a.co.uk/folder1/page1

So where do the following point?


So with those questions still left out there, I expect what you want is something like this (note using https so the forum software doesn't change it into a live link).

RewriteCond %{HTTP_HOST} ^(www\.)?b\.com$ [NC]
RewriteRule .* https://a.co.uk/THISPAGE1 [R=301]

RewriteRule ^THISPAGE1$ folder1/page1 [L]

This basically says
- if the request URL is b.com with/without www and with/without a path beyond the domain
- then redirect to https://a.co.uk/some-custom-landing-page
- and throw away anything in the request path aside from the domain
- but point that address to another place on the file system, namely at a.co.uk/folder1/page1 (using a path relative to the present location of the .htaccess file, which may or may not be correct).

But that makes many assumptions about what you want.


 11:22 am on Feb 25, 2013 (gmt 0)

Thanks ergophobe. That helped a lot.
Unfortuanltely RewriteRule ^THISPAGE1$ folder1/page1 [L] did not work.

Let me give more info.

I have 4 domains pointing to the same site.
The main domain: a.com
and 3 other domains ( b.com, c.com, d.com)taking to landing pages.

If users try to go to b.com/folderx/pagey or any other path within b.com, they should be redirected to the b.com related landing page (same for c and d). Note that the landing page might be quite deep in a folder structure such as folder1/folder2/folder3/folder4/folder5/page . so , to avoid the display of suche horrible URL, I want to present something prettier such as a.com/landing_page_related_to_b .

What I did not say earlier is that the system works off a content management system so the folders and pages do not actually physically exist as physical files on the server. It is all stored in a database.

Using what you gave me, it work fine up to the redirect. but the internal rewrite shows me "page not found"


 11:30 am on Feb 25, 2013 (gmt 0)

The target of the rewrite needs to be the physical internal server location where the content actually resides, not the old URL for that content.

URLs are used out on the web. Files and folders are used inside the server. They are not at all the same thing, merely related by the server configuration.

If the old URL was
and it was served (by way of a rewrite) by the file at
and you want to use a URL like
instead, you now need to:
1. redirect requests for example.com/folder1/folder2/page to www.example.com/page
2. redirect requests for example.com/index.php?param1=folder1&param2=folder2&pagename=page to www.example.com/page
3. rewrite requests for www.example.com/page to /index.php?param1=folder1&param2=folder2&pagename=page

Notice that a redirect maps a URL to a URL and a rewrite maps a URL to an internal location.


 12:16 pm on Feb 25, 2013 (gmt 0)

okay... good point
So I have now

RewriteCond %{HTTP_HOST} ^(www\.)?b\.com$ [NC]
RewriteRule .* https://a.co.uk/THISPAGE1 [R=301]
RewriteRule ^THISPAGE1$ /index.php?param1=folder1&param2=folder2&pagename=page [L]

I am not surte whether the redirect should be done on the third or second line?


 12:59 pm on Feb 25, 2013 (gmt 0)

Add a blank line after each Rule so you can see what code is processed together. Conditions apply only to the single following Rule.

As coded, a request for b.com/<anything> with or without www is redirected to a.co.uk/THISPAGE1

The new request for anyhostname.tld/THISPAGE1 is then served by the internal script.

Every rule needs the [L] flag.


 2:13 pm on Feb 25, 2013 (gmt 0)

That got it perfectly.
The L rule was missing.

Final code is:

RewriteCond %{HTTP_HOST} ^(www\.)?b\.com$ [NC]
RewriteRule .* https://a.co.uk/THISPAGE1 [R=301,L]

RewriteRule ^THISPAGE1$ /index.php?param1=folder1&param2=folder2&pagename=page [L]

and it works. And I understand how the 2 redirects work now.

Many thanks guys.


 2:43 pm on Feb 25, 2013 (gmt 0)

There's not two redirects.

There's one redirect and one rewrite. It's very important you understand that difference.


 2:55 pm on Feb 25, 2013 (gmt 0)

okay. got you.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved