Search code examples
.htaccesshttp-redirecthttp-status-code-301

301 redirect both www and non-www to fake directory


I want my site to always redirect to a default language if one is not already requested by the user — i.e. if the default language is English, I want to redirect the user to http://domain.com/english/ when he/she enters http://domain.com/.

At the same time, I want to remove the 'www' part from the URL, as well as redirect all traffic through http://domain.com/index.php.

I have the following .htaccess file, which is working great when the user enters www.domain.com, but not when the user enters domain.com (without 'www'). What am I doing wrong?

#
# Remove WWW from URL
#

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

#
# Redirect all traffic through index.php
#

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

Solution

  • Try this:

    # Remove WWW from URL and redirect to /english/
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR]
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule ^(.*)$ http://example.com/english/$1 [R=301,L]