Mr. B.
Mr. B.

Reputation: 8697

htaccess: redirect 301 doesn't work properly

This code is working fine:

RewriteEngine On
RewriteBase /my/project/dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /my/project/dir/index.php?uri=$1 [QSA,L]

But now I'd like to force the www. within the URL to avoid duplicate content (SEO). I found this code-snippet, but I'm not able to integrate it into my code:

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

I tried a few combinations but I don't want to confuse you. None of them was working.

How would you extend the first (working) code lines to accomplish the goal? Thanks in advance!

Upvotes: 1

Views: 269

Answers (1)

anubhava
anubhava

Reputation: 785146

Change the order of rules and use %{REQUEST_URI} in 301 rule:

RewriteEngine On
RewriteBase /my/project/dir/

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /my/project/dir/index.php?uri=$1 [QSA,L]

Upvotes: 1

Related Questions