user3602420
user3602420

Reputation: 127

.htaccess add www. doesn't work like expected

I want to add a www. to my domain. If someone goes to 'mydomain.com' it needs to be send to 'www.mydomain.com'. This all works fine. The problem is if someone types in 'mydomain.com/subfolder/subfolder3' it redirects to 'www.mydomain.com/mydomain/subfolder/subfolder3' instead of 'www.mydomain.com/subfolder/subfolder3'

My .htaccess is located in the htdocs folder beside the www folder.

My .htaccess:

php_flag display_errors Off

php_value upload_max_filesize 25M
php_value post_max_size 25M

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/ [R=301,L]

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

AuthUserFile /home/bla/bla/bla/domains/mydomain/htdocs/beta/.htpasswd
AuthGroupFile /dev/null
AuthName "WebDav only for authorized users"
AuthType Basic

<LimitExcept GET POST HEAD>
require valid-user
</LimitExcept>

As you can see there are 2 domains. If someone types in the second domain it also needs to redirect to the first domain.

The 'AuthUserFile' you can see doesn’t exist anymore. Can I safely delete everything beneath it?

Upvotes: 1

Views: 42

Answers (1)

anubhava
anubhava

Reputation: 785128

You can do this in a single rewrite rule like this:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(mydomain|(www\.)?myseconddomain)\.com$ [NC]
RewriteRule ^ http://www.mydomain.com%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.myseconddomain\.com$ [NC]
RewriteRule ^ http://%1.mydomain.com%{REQUEST_URI} [R=301,L]

Upvotes: 0

Related Questions