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/.
A开发者_如何学Ct 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]
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]
Try
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
With ?
to make the www optional so the first rule catches both.
Edit:
Use this rule to remove the www., then use a redirect on the index.php page to go to /english/ instead of a url rewriting rule.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
I think the following should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/english/$1 [R=301,L]
You're saying if the host is not exactly www.example.com, then redirect to it.
精彩评论