Forum Moderators: phranque

Message Too Old, No Replies

redirecting all subdomains to same subdomain on different domain

.htaccess problem

         

IanTurner

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

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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.

lucy24

6:38 pm on Dec 6, 2016 (gmt 0)

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



<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).

IanTurner

6:56 pm on Dec 6, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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.

w3dk

9:42 pm on Dec 6, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



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]+\.)

phranque

10:46 pm on Dec 6, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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

or at the least something like:
([^.]+\.)

lucy24

12:35 am on Dec 7, 2016 (gmt 0)

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



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.

phranque

12:58 am on Dec 7, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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]

lucy24

8:01 pm on Dec 7, 2016 (gmt 0)

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



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\.)+

IanTurner

8:42 am on Dec 8, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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.