Forum Moderators: phranque

Message Too Old, No Replies

Redirects

Tried combining a few, didn't work.

         

chronos

5:12 pm on Feb 15, 2008 (gmt 0)

10+ Year Member



On the server I am hosting a few personal sites, I have the main domain using a basic CMS, very basic. But when I put in a link from it's main page to my addon domain, it shows the URL as www.maindomain.com/addondomain

I want it to just direct to www.addondomain.com

So I put this in my .htaccess file thinking it would work:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^addondomain.maindomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.addondomain.maindomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^maindomain.com/addondomain$ [OR]
RewriteCond %{HTTP_HOST} ^www.maindomain.com/addondomain$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* [addondomain.com...] [L,R=301]

The subdomain redirects work, but the /folder redirects don't. Any tips? Yes, I did replace addondomain and maindomain with the actual domain names.

wilderness

5:19 pm on Feb 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try escaping "\" all those dots in the HOST lines.

chronos

6:35 pm on Feb 15, 2008 (gmt 0)

10+ Year Member



Tried:
RewriteCond %{HTTP_HOST} ^addondomain.\maindomain.\com$ [OR]
RewriteCond %{HTTP_HOST} ^www.\addondomain.\maindomain.\com$ [OR]
RewriteCond %{HTTP_HOST} ^maindomain.\com/addondomain$ [OR]
RewriteCond %{HTTP_HOST} ^www.\maindomain.\com/addondomain$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* [addondomain.com...] [L,R=301]

But it didn't work. Did I do it wrong?

wilderness

6:46 pm on Feb 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



the escapes go prior to the dots as that is what your escaping.

chronos

6:56 pm on Feb 15, 2008 (gmt 0)

10+ Year Member



Sorry, did that but it didn't work.

wilderness

7:24 pm on Feb 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You'll need to wait for Jim or another to check the validity of your lines.

The escapes are required.

Also not sure to the validity of your leading "www." as opposed to the complete URL's.

chronos

7:47 pm on Feb 15, 2008 (gmt 0)

10+ Year Member



I've used this before and it works just fine:

RewriteCond %{HTTP_HOST} ^addondomain.maindomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.addondomain.maindomain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* [addondomain.com...] [L,R=301]

Just breaks when I put in the lines for redirecting from maindomain.com/folder

wilderness

4:17 pm on Feb 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



bump

jdMorgan

10:41 pm on Feb 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The reason this does not work is that "/addondomain" is part of the local URL-path, and will never appear in the value of %{HTTP_HOST}. You must check for "addondomain" in the RewriteRule pattern instead. This means that you'll need two separate rules to accomplish your goals.

Also note that


RewriteCond %{HTTP_HOST} ^addondomain.maindomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.addondomain.maindomain.com$

Can be more efficiently and robustly written as a single RewriteCond:

RewriteCond %{HTTP_HOST} ^(www\.)?addondomain\.maindomain\.com(:[0-9]{1,5})?$

This makes the "www." optional, and allows for an optional port number to be (validly) appended.

Jim

chronos

2:00 pm on Feb 18, 2008 (gmt 0)

10+ Year Member



Thank you. I'll check into the RewriteRule part as you said.

Achernar

2:24 pm on Feb 18, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Don't you think that the best approach would be to correct the link in the CMS?
You should be able to link to different domains.

wilderness

3:52 pm on Feb 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Jim

Your a star Jim, many thanks.

Unfortuantely your spread thinner than peanut butter!
Are you in the clone bank ;)

Don

chronos

3:31 pm on Feb 25, 2008 (gmt 0)

10+ Year Member



Achernar -
I was working on that, but at the same time, it can still be accessed by manually typing that URL in, which is what I want to prevent.

jdMorgan

9:32 pm on Feb 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tlooks to me like you actually need two rules:

# Redirect direct client requests for /addondomain to www.addondomain.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /addondomain
RewriteRule ^addondomain http://www.addondomain.com/ [R=301,L]
#
# Redirect to canonicalize addondomain.maindomain.com and
# www.addondomain.maindomain.com to www.addondomain.com
RewriteCond %{HTTP_HOST} ^(www\.)?addondomain\.maindomain\.com
RewriteRule (.*) http://www.addondomain.com/$1 [R=301,L]

Jim