views:

63

answers:

2

Hi I have an application which uses opencart. I would like to make a 301 reditect in case the user types http://example.com. To be redirected in http://www.example.com (301 status code)

Here is my .htaccess content:

RewriteEngine On

\#OPENCART REWRITES START

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*) index.php

\#OPENCART REWRITES END

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]

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

I get 302 redirection instead of 301.

Thanx, Granit

+1  A: 

Have you tried doing:

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

Emphasis on the second line, as it matches against http://example.com as opposed to matching against anything-but www.example.com, which will break if you happen to use subdomains. I'm not sure if this is exactly related to your 301/302 issue, but it could have an affect. Also, try on your Rule [R=301,NC,L].

squeeks
Thank you for your response.Unfortunately it does not work!
Granit
That's awfully strange!I wonder if there is other rewrite rules causing issues with it.
squeeks
A: 

Try it with a different order. Put your rules that cause an external redirect before those that only cause an internal redirect:

RewriteEngine On

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

#OPENCART REWRITES START
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
#OPENCART REWRITES END
Gumbo