.Htaccess Redirect Non-Www to Www Preserving Uri String

.htaccess Redirect non-WWW to WWW preserving URI string

It should be something like this:

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

Redirect non-www to www in .htaccess

Change your configuration to this (add a slash):

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

Or the solution outlined below (proposed by @absiddiqueLive) will work for any domain:

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

If you need to support http and https and preserve the protocol choice try the following:

RewriteRule ^login\$ https://www.%{HTTP_HOST}/login [R=301,L]

Where you replace login with checkout.php or whatever URL you need to support HTTPS on.

I'd argue this is a bad idea though. For the reasoning please read this answer.

Generic htaccess redirect www to non-www

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

Same as Michael's except this one works :P

codeigniter redirect non-www to www

Try, in index.php, at the beginning:

if(substr($_SERVER['SERVER_NAME'],0,3)=="www"){
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://yourdomain.tdl/".$_SERVER['REQUEST_URI']);
}else{
//the current contents of your file
}

EDIT
I read your question wrong, the answer is:

if(substr($_SERVER['SERVER_NAME'],0,3)!="www"){
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.yourdomain.tdl/".$_SERVER['REQUEST_URI']);
}else{
//the current contents of your file
}

.htaccess www to non-www while keeping path, ssl, and query string

You need to check if a request is http or https, and redirect to the correct protocol. Right now, you're redirecting everything to http://. You can use this condition and grouping:

RewriteCond %{HTTPS}:s (on:(s)|off:s)

This uses the %{HTTPS} variable, which is either "on" or "off", and pairs it with an s. Then we match either on:(s) or off:s. Therefore, if HTTPS is "on", the (s) gets grouped and we can backrefernce it using a %2, otherwise, if HTTPS is "off", nothing gets grouped and %2 is blank. We can then use it in your redirect:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTPS}:s (on:(s)|off:s)
RewriteRule ^(.*)$ http%2://domain.com/$1 [R=301,L]

As for the second thing about the paths not being preserved, it sounds like your rules are in the directory /path, which means it gets stripped off when it gets sent through these rules. You need to move them to your document root. Alternatively, if you must have your rules in the /path directory, you can use the %{REQUEST_URI} variable instead:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTPS}:s (on:(s)|off:s)
RewriteRule ^ http%2://domain.com%{REQUEST_URI} [R=301,L]


Related Topics



Leave a reply



Submit