Mobile app version of vmapp.org
Login or Join
Candy875

: .htaccess rewrite rules not working 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

@Candy875

Posted in: #Htaccess #MultiSubdomains #NoWww #Redirects #Subdomain

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: moz.com/learn/seo/redirection
This is the rule Moz is recommending:

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


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

RewriteCond %{HTTP_HOST} *!^www*.example.com [NC]
RewriteRule (.*) www.example.com/ [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?

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Candy875

3 Comments

Sorted by latest first Latest Oldest Best

 

@Smith883

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.

10% popularity Vote Up Vote Down


 

@LarsenBagley505

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

RewriteEngine On
RewriteRule ^(.*)$ www.example.com/ [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: 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: 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.

10% popularity Vote Up Vote Down


 

@Alves908

Try:

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


This should work.

Here is mine:

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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme