Amy Neville
Amy Neville

Reputation: 10621

.htaccess ensure www redirect incorrect

I am trying to ensure that urls always have www in front of them for canonicalization reasons.

Unfortunately when I put the following url in:

http://website.net/handbags/1/12/this-is-some-text

It redirects me here:

http://www.website.net/?controller=handbags&path=1/12/this-is-some-text

I would like to add it works fine when using:

http://www.website.net/handbags/1/12/this-is-some-text

I want it to redirect me here...

http://www.website.net/handbags/1/12/this-is-some-text

I'm not sure what could be causing this error. Here is my .htaccess file:

<IfModule mod_rewrite.c>

    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # CORE REDIRECT
    RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]

    # ENSURE WWW
    RewriteCond %{HTTP_HOST} !^www\.               [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{2,6})$ [NC]
    RewriteRule ^(.*)$       http://www.%1/$1      [R=301,L]

</IfModule>

Upvotes: 1

Views: 80

Answers (2)

Nick Mallare
Nick Mallare

Reputation: 180

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

    # ENSURE WWW
    RewriteCond %{HTTP_HOST} !^www\.               [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{7})$ [NC]
    RewriteRule ^(.*)$       http://www.%1/$1      [R=301,L]

    # CORE REDIRECT
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
</IfModule>

You can do what I've done above; however, it's just going to redirect to http://www.website.net/handbags/1/12/this-is-some-text and then it's immediately going to hit the "CORE REDIRECT" rule and send you to http://www.website.net/?controller=handbags&path=1/12/this-is-some-text.

Is there a reason you don't want it to redirect to "?controller=handbags" when they go to www?

UPDATE

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

    # ENSURE WWW
    Rewritecond %{HTTP_HOST} !^www\.website\.net
    RewriteRule ^(.*)$       http://www.website.net/$1      [R=301,L]

    # CORE REDIRECT
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
</IfModule>

Upvotes: 1

Johannes H.
Johannes H.

Reputation: 6167

Whenever your # CORE REDIRECT matches, the other rules are not even checked, as the core redirect rule has the Lflag set. That ones tells Apache to stop trying other rules. Remove that flag or change the rule. ;)

Upvotes: 0

Related Questions