Welcome to WebmasterWorld Guest from 35.153.135.60

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

redirecting all subdomains to same subdomain on different domain

.htaccess problem

     
11:46 am on Dec 6, 2016 (gmt 0)

Moderator from GB 

WebmasterWorld Administrator ianturner is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 19, 2001
posts: 3665
votes: 54


I need to redirect all traffic to example.tld to example.com without changing the subdomain

so that foo.example.tld goes to foo.example.com, bar.example.tld goes to bar.example.com

I'm not sure how to wild card the first part of the redirect condition.
6:38 pm on Dec 6, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15699
votes: 810


<boilerplate>
What have you tried so far?
</boilerplate>
One option would be a capture from an %{HTTP_HOST} condition. Be careful about what order you put your conditions in, since you can only capture from the last met condition, and the www. subdomain may need different handling (depending on site preferences).
6:56 pm on Dec 6, 2016 (gmt 0)

Moderator from GB 

WebmasterWorld Administrator ianturner is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 19, 2001
posts: 3665
votes: 54


Have got this working for www and for the non-www but don't know how to

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

but just changing wwws to * didn't seem to work... so I can write individual redirects for all my subdomains but wanted to manage it in one.
9:42 pm on Dec 6, 2016 (gmt 0)

New User

joined:Mar 17, 2016
posts: 14
votes: 3


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


This wouldn't actually work in .htaccess (for anything but the document root). The URL-path matched by the RewriteRule pattern does not include the directory prefix, so the $1 backreference does not start with a slash. This would, however, be OK if used directly in the server config.

but just changing wwws to * didn't seem to work...


* or .* (dot-asterisk)?

A single * would result in an invalid regex and this will break horribly (500 error).

You could do something like the following to handle "everything" (www, apex domain and all sub domains):


RewriteCond %{HTTP_HOST} ^(.+\.)?example\.uk$ [NC]
RewriteRule (.*) http://%1example.co.uk/$1 [R=301,L]


If you know your subdomains are of a particular format (eg. letters only perhaps?) then you should make the subdomain pattern as restrictive as possible. eg. ([a-z]+\.)
10:46 pm on Dec 6, 2016 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11714
votes: 210


you should make the subdomain pattern as restrictive as possible. eg. ([a-z]+\.)

or at the least something like:
([^.]+\.)
12:35 am on Dec 7, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15699
votes: 810


The subdomain should always be reducible to \w+. (Are you even allowed to use hyphens in subdomain names? At worst it would then be [\w-]+.)

I don't think you can do it without two rules. If your default name is with-www, as implied by your example, you'e have:
RewriteCond %{HTTP_HOST} ^(\w+)\.example\.uk
RewriteRule (.*) http://%1.example.co.uk/$1 [R=301,L]
immediately followed by
RewriteCond %{HTTP_HOST} ^example\.
RewriteRule (.*) http://www.example.co.uk/$1[R=301,L]
Note that in the second rule nothing is said about TLD; the same rule applies to both "example.uk" and "example.co.uk" since both are now wrong.

If your default name is without-www, so there's nothing before "example" except in subdomains, it gets a little more fiddly.

Edit: The second rule is probably not optimal. If you need to protect against hijackers, express the condition as !^\w+\.example\. instead.
12:58 am on Dec 7, 2016 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11714
votes: 210


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


example.example.co.uk would get incorrectly redirected with these rulesets.
maybe this:
RewriteCond %{HTTP_HOST} ^example\.(co\.)?uk$
RewriteRule (.*) http://www.example.co.uk/$1[R=301,L]
8:01 pm on Dec 7, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15699
votes: 810


example.example.co.uk would get incorrectly redirected

Eeuw. Do you think this is something that might actually occur? (Admittedly, there doesn't seem to be much limit to what search engines will try in the interest of entrapment.) I don't think OP talked about wild-card subdomains, one way or the other. If there's only a small number of possible subdomains, the Condition for the subdomain rule would be better expressed as
^(www|mobile|shop|blog)\.example

Alternately, the without-subdomain rule could go before the with-subdomain one, and then you could express the condition as
^(example\.)+
8:42 am on Dec 8, 2016 (gmt 0)

Moderator from GB 

WebmasterWorld Administrator ianturner is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 19, 2001
posts: 3665
votes: 54


Yes I did the without subdomain rule as a separate entity.

I was trying to catch all subdomains as a wildcard for the domain to domain redirect as the original domain already has its rules in place.