Rich
Rich

Reputation: 426

.htaccess subdomain redirect

My server is somehow configured to accept any string as a existing subdomian. This subdomains doesn't redirect, they show content instead. For example:

bla.example.com // shows the content of http://example.com

blabla.example.com // show the content of http://example.com

lorem.example.com/events/ // shows the content of http://example.com/events

So you can use "stackoverflow.example.com" and it will work.

I want to create an .htaccess rule to overwrite this behavior. I want to redirect http://*.example.com/* to http://www.example.com/*

Everything I have done so far is redirect http://example.com to http://www.example.com (wich is another behavior I want to keep)

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

Thank you for your help!

Upvotes: 0

Views: 578

Answers (1)

Ulrich Palha
Ulrich Palha

Reputation: 9509

Try adding the following to your htaccess file in the root folder of your domain

RewriteEngine on
RewriteBase /

#if its not www.example.com
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
#redirect it to www.example.com
RewriteRule .* http://www.example.com%{REQUEST_URI} [R=301,L]

Upvotes: 2

Related Questions