0

I need to redirect multiple subdomains to a single subdomain (www) and have found what seems to be a really good guide exactly on point: http://moz.com/learn/seo/redirection

This is the rule Moz is recommending:

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

So I used the rule with my own domain as follows:

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

... which results in the following error:

example.com

HTTP/1.1 500 Internal Server Error
Date: Mon, 27 Apr 2015 23:10:41 GMT
Server: Apache
Vary: Accept-Encoding
Content-Length: 674
Connection: close
Content-Type: text/html; charset=iso-8859-1

What could be wrong?

  • FWIW the asterisk prefix on the *!^www*.example\.com CondPattern is likely to be causing your 500 error. The second asterisk in that pattern is also an obvious error. (TBH that almost looks like some MarkDown/formatting gone wrong?!) Unfortunately there seems to be a few mistakes and misinformation in that linked article. – MrWhite Apr 28 '15 at 15:25
2

Try:

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

This should work.

Here is mine:

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
  • 1
    Hi closetnoc. Do you know why Moz wrote their rule the way they did? They are a reputable resource for webmasters, and the page itself is quoted a lot elsewhere, so I'm wondering why their syntax is working for others and not for me. P.S. Your edited version didn't work either. – Knocks X Apr 27 '15 at 23:27
  • 1
    @KnocksX Not sure why. Sites like MOZ get quoted a lot even when they are completely wrong. I like MOZ, but quibble with some of what they are putting out. Still, I use that site a lot for information, but take it with salt. The syntax in the regex (regular expression) was just plain wrong. For example, they were using a wildcard (*) incorrectly just before the not (!) which should be the first character. As well, they did not escape () the first (.) in the domain name just after the www. I suspect they were trying to be more generic with the www assuming room for www1 etc. – closetnoc Apr 27 '15 at 23:33
  • 1
    Those subdomains don't exist, but they existed under the previous owner of the domain. That's the reason I need a rule that will redirect all possible subdomains (I don't know how many and which ones existed before) as a "catch-all." I know this is possible, because domain parking companies do it all the time in order not to lose even a single visitor. You can just type gibberish.example.com, and the visitor will be taken to the index. – Knocks X Apr 28 '15 at 1:31
  • 1
    You won't be able to without the web server knowing where to place the request. You can simply add a ServerAlias archive.example.com (for example) in your Apache configuration file. It will require a restart. As well, you will need to create a CNAME for each sub-domain pointing to example.com. Your example gibberish.example.com is likely using a catch-all site which has to be the first site created on a server. It may be possible to create one after the fact, but I would opt for the method above rather than trying the latter. It is just simpler. – closetnoc Apr 28 '15 at 1:38
  • 1
    Except that a catch-all will not work without having a sub-domain defined in the DNS. You can use a wildcard sub-domain in the DNS. Creating a catch-all site is not easy. By default, the first site created could be used for a catch-all. I will look into this and see what I can figure out. It has been a number of years since I fiddled specifically with a catch-all short of using my first site created 8 years ago. – closetnoc Apr 28 '15 at 2:34
0

The best way to go is to create an .htaccess file with the following contents:

RewriteEngine On
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Then copy that file to the document root of each domain. Normally a document root is right in the public_html if your web server is setup like most.

Then use redbot.org and test everything by plugging in each domain.

You should then see at least the following lines for the headers:

HTTP/1.1 301 Moved Permanently
Location: http://www.example.com/

If you want another way to do redirects, then don't use .htaccess and don't deal with rewrite rules. Instead, create a script that does redirects. A modern way to do it in PHP is to use the following script.

<?php
header("HTTP/1.1 301 Moved Permanently",true);
header("Location: http://www.example.com/",true);
?>
<html>
<head>
<title>Moved</title>
</head>
<body>
<a href="http://www.example.com">click here to continue.</a>
</body>
</html>

If you decide to take on the PHP route, then you need to save the above as the default PHP filename configured for your server which would normally be index.php, and you will have to place that file in every single folder of the subdomain thats meant to be redirected.

  • There is more to this question that is not within the question. The OP was trying to apply some of what I offered to sub-domains that do not exist anymore. Waiting to hear from the OP. (see comments) It sounds like they need a wild-card sub-domain in the DNS and a catch-all site (which may already exist). If you want to address this, that would be GREAT!! The OP is not as tech-savvy as you and I, though absolutely fantastic! Any answer that covers this new topic should be somewhat detailed. – closetnoc Apr 28 '15 at 16:46
0

This may be a silly suggestion, but are you sure the site you are using is linux and uses a htaccess files?

I recently inherited some web properties, only to realise that despite having a htaccess file, changes I was making weren't being applied as the server was Windows, and not linux.

  • 1
    The .htaccess file wouldn't have triggered a 500 error unless the .htaccess file was being read. However, if you read the comments on closetnoc's answer you will realise there is more to this problem than stated in the question... it seems the server is not actually set up to receive requests to these subdomains! – MrWhite Apr 30 '15 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.