Forum Moderators: phranque

Message Too Old, No Replies

htaccess rule for redirecting typos to proper subdomain

         

ntbgl

2:20 am on Mar 26, 2009 (gmt 0)

10+ Year Member



I have a domain I use, such as example.org

On this domain, not only do I use the www subdomain, but I also use several other subdomains.

Beyond my primary domain, I also have example.com, and a list of six other spelling and .com/.org variants as well.

Using a wildcard DNS setting, my hosting company has redirected all the spelling variants to a folder on my account I called "redirect". The hosting company said I can write an .htaccess file in that subfolder to redirect my traffic accordingly.

I've started with an old example.org => www.example.org www .htaccess rule I made several years ago.

This is what I have now.

RewriteCond %{HTTP_HOST} !example\.org$ [NC]
RewriteRule (.*) [www\.example\.org...] [R=301,L]

I need help. I don't want that www subdomain to be there, I want it to be dynamic, based on whatever subdomain they specified.

Any help would greatly be appreciated.

g1smd

3:11 am on Mar 26, 2009 (gmt 0)

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



You to be more specific with this description.

Do you have multiple sub-domains on each domain?

Does each TLD (com¦net¦org) serve up the same content, given that we look at a single sub-domain each time?

What do you want to happen for the non-www version for each the domains?

There are going to be multiple ways to encode this, it's a matter of first finding out exactly what it needs to do.

I assume the problem is "keep the same sub-domain name and filepath while redirecting to a canonical domain name and TLD".

ntbgl

3:41 am on Mar 26, 2009 (gmt 0)

10+ Year Member



I need my .htaccess file to redirect from this to this:

example.com => www.example.org
example.com/file.html => www.example.org/file.html

www.example.com => www.example.org
www.example.com/file.html => www.example.org/file.html

sub.example.com => sub.example.org
sub.example.com/file.html => sub.example.org/file.html

exaampple.org => www.example.org
exaampple.org/file.html => www.example.org/file.html

www.exaampple.org => www.example.org
www.exaampple.org/file.html => www.example.org/file.html

sub.exaampple.org => sub.example.org
sub.exaampple.org/file.html => sub.example.org/file.html

g1smd

10:59 am on Mar 26, 2009 (gmt 0)

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



Something like this:

# Redirect ^example.org to www.example.org 
#
RewriteCond %{HTTP_HOST} ^example\.org [NC]
RewriteRule (.*) http://www.example.org/$1 [R=301,L]
#
# Above Rule probably isn't needed. Next rule below now covers it I think.
#
#
#
# Redirect root domains [domain.(aa.)?bb] to www.example.org - works for .com and .co.uk
# Note that sub-domains will be handled by a different rule.
#
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2}\.)[b]?[/b]([a-z]{2-4})\.[b]?[/b](:[0-9]+)[b]?[/b]$ [NC]
RewriteRule (.*) http://www.example.org/$1 [R=301,L]
#
#
#
# Redirect subdomains [sub.domain.(aa.)?bb] to same-sub at sub.example.org - works for .com and .co.uk
# but not for something.example.org (would be redirecting to self)
#
RewriteCond %{HTTP_HOST} !([^.]+\.)example\.org [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.([a-z]{2}\.)[b]?[/b]([a-z]{2-4})\.[b]?[/b](:[0-9]+)[b]?[/b]$ [NC]
RewriteRule (.*) http://%1.example.org/$1 [R=301,L]

Not tested, no warranties. Probably overlooked something.

You need to test every URL format that could be thrown at the server to ensure it redirects properly and does not create an infinite loop.

Flush browser cache before testing.

Code will fail if any of your domain names are just two letters long, like www .me.com for example.

ntbgl

4:33 pm on Mar 26, 2009 (gmt 0)

10+ Year Member



Thanks for the reply but it doesn't seem to affect anything. I'll have to investigate further.

g1smd

4:49 pm on Mar 26, 2009 (gmt 0)

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



The above code was meant to replace whatever you already had. If you merely added it to what you already had then it would always fail.

You'll need

RewritEngine On
and all the usual stuff ahead of the redirects.

ntbgl

11:49 pm on Mar 26, 2009 (gmt 0)

10+ Year Member



My htaccess file looks like this:

***

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} !([^.]+\.)example\.org [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.([a-z]{2}\.)?([a-z]{2-4})\.?(:[0-9]+)?$ [NC]
RewriteRule (.*) [%1.example.org...] [R=301,L]

***

With that, nothing happens.

If I make my .htaccess file look like this:

***

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} !([^.]+\.)example\.org [NC]
RewriteRule (.*) [%1.example.org...] [R=301,L]

***

Then the following examples redirect with an error to:

http://example.com/ => [.example.com...]
http://www.example.com/ => [.example.com...]

If I change my .htaccess to this:

***

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} !^(.*)example\.org [NC]
RewriteRule (.*) [%1example.org...] [R=301,L]

***

Thank most things work, except the sub domain in the RewriteCond dosen't get passed to the RewriteRule.

g1smd

12:00 am on Mar 27, 2009 (gmt 0)

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



***
RewriteCond %{HTTP_HOST} [b]![/b]([^.]+\.)example\.org [NC]

The ! means NOT, and you are asking for something to NOT match a pattern in this condition.

When that is true, the %1 back-reference is always going to be empty.

That's why nothing is passed. That is exactly how it is coded.

You'll see that in my code, I source the %1 back-reference from the second RewriteCond within the rule, because that pattern is coded as a positive match.

.

My original code is closer to what you need, but I am unsure why it fails - except from the example you gave, it appears that you have only copied a part of it over.

<edit>

Spotted the typo. In two places, change

{2[b]-[/b]4}
to
{2[b],[/b]4}
like this:

# Redirect ^example.org to www.example.org 
#
RewriteCond %{HTTP_HOST} ^example\.org [NC]
RewriteRule (.*) http://www.example.org/$1 [R=301,L]
#
# Above Rule probably isn't needed. Next rule below now covers it I think.
#
#
#
# Redirect root domains [domain.(aa.)?bb] to www.example.org - works for .com and .co.uk
# Note that sub-domains will be handled by a different rule.
#
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2}\.)[b]?[/b]([a-z]{2[b],[/b]4})\.[b]?[/b](:[0-9]+)[b]?[/b]$ [NC]
RewriteRule (.*) http://www.example.org/$1 [R=301,L]
#
#
#
# Redirect subdomains [sub.domain.(aa.)?bb] to same-sub at sub.example.org - works for .com and .co.uk
# but not for something.example.org (would be redirecting to self)
#
RewriteCond %{HTTP_HOST} !([^.]+\.)example\.org [NC]
RewriteCond %{HTTP_HOST} ^[b]([/b][^.]+[b])[/b]\.([^.]+)\.([a-z]{2}\.)[b]?[/b]([a-z]{2[b],[/b]4})\.[b]?[/b](:[0-9]+)[b]?[/b]$ [NC]
RewriteRule (.*) http://[b]%1[/b].example.org/$1 [R=301,L]

ntbgl

12:42 am on Mar 27, 2009 (gmt 0)

10+ Year Member



it appears that you have only copied a part of it over.

I feel so embarrassed, that's exactly what went wrong. I saw how the lines repeated and grew in length, I thought this was an explanation of the way the code developed.

The code, in it's entirety, works beautifully, thank you so much, and thanks for having a lot a patience!

FYI, the first bit about the non www to www was covered by the next part of the code.

Thanks again!

g1smd

12:48 am on Mar 27, 2009 (gmt 0)

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



The two typos (hyphens for commas) would also have stopped it working anyway, so I am glad you posted that there was a problem.

I probably wouldn't have spotted that otherwise.

OK about the first rule being redundant. I thought as much.

I can't see a way to combine the two remaining rules into one. I think this is about as efficient as I can get it.

ntbgl

1:06 am on Mar 27, 2009 (gmt 0)

10+ Year Member



I do have one further question.

example.org is mapped to a version specific directory on my hosting account. /opensourcesoftware.1.2.1/

And all my misspelt domains are mapped to a redirect folder. /redirect/

That way, if a new version of the software comes out, I can install it, set everything up, then when I'm ready to go live, I just need to remap example.org to the new version. I won't have to remap all of the domains.

Right now, for this .htaccess file to work, I have to have two copies of it, one in /opensourcesoftware.1.2.1/ and one in /redirect/

I would love to just put this in the root / but I don't know what to set the rename base to:

RewriteBase /opensourcesoftware.1.2.1/
RewriteBase /opensourcesoftware.1.2.1
RewriteBase opensourcesoftware.1.2.1/

Don't seem to work. Might you have any suggestions?

g1smd

1:29 am on Mar 27, 2009 (gmt 0)

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



Once the redirects are working for all requests then you might no longer need all the alternative names to be pointing at a separate folder.

You might be able to point all of the names at the one single folder where the content really resides, and the redirects will sort out correct access for the user.

g1smd

7:13 pm on Mar 27, 2009 (gmt 0)

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



I have been reminded to say that the code does not cater for hyphenated domains, nor for TLDs with more than four characters, but both of those are quite easy to fix.

Caterham

8:07 pm on Mar 27, 2009 (gmt 0)

10+ Year Member



I would love to just put this in the root /

Can't see a good reason to do it (intercepting outside of the documentRoot) but you'll have to specify (=strip) the folder which is not part of the URL-path like

RewriteCond %{HTTP_HOST} ^example\.org [NC]
RewriteRule ^opensourcesoftware\.1\.2\.1/(.*) http://www.example.org/$1 [R=301,L]

but I don't know what to set the rename base to:

RewriteBase /opensourcesoftware.1.2.1/

If you're using filepaths in the substitution (i.e., not an URL nor an URL-path) you'd have to specify

RewriteBase /