Redirect all http/www into https://www - regex

I have this code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
What this does is:
With http://example.com it redirects to https://www.example.com (this is correct)
With https://example.com it redirects to https://www.example.com (this is correct)
But with http://www.example.com it doesn't redirect to https://www.example.com.
Please take note that It needs to be not more than 1 chain. It should redirect to https://www.example.com.

You can avoid hardcoding your host name and do both conditions in a single redirect rule like this:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

Okay I just fixed it.
this is the updated code
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
I can guarantee that this will all do 301 redirect without more than 1 chain.
I hope this helps some developers out there!.

Related

How to exclude subdomains from redirecting?

I have this code to force any URL to be "https://www.":
#rewrite to WWW:
RewriteCond %{HTTP_HOST} !^(\d+)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTP_HOST} !^(\d+)
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have sub-domains and I do not want any of them to be redirected. What can I do?
You can do it all in single rule:
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(?:static\.|subdomain1\.|subdomain2\.|\d+) [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
Make sure to clear your browser cache before testing this rule.

apache .htaccess rewrite should not include www

We have a domain test.com, we want to redirect http://www.test.com to http://test.com and https://www.test.com to https://test.com, We can achieve the same by below mentioned rule, but we dont want http:// and beside what do we have to do for https://
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
You can have a single rule to handle both http and https traffic:
RewriteEngine On
RewriteCond %{HTTP_HOST}#%{HTTPS}s ^www\.([^#]+)#(?:off|on(s)) [NC]
RewriteRule ^ http%2://%1%{REQUEST_URI} [R=302,L,NE]
If you want everything to go to https, then you need to change your rule to:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$ [NC]
RewriteRule ^(.*)$ https://test.com/$1 [R=301,L]
Otherwise, you'll need separate rules for http and https:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

htaccess redirect conflict/not working

I have a QR code that I need to redirect to a new page. For some reason the redirect is being overridden (ends up at cmdgroup.com/community instead of the appropritate page)
RewriteCond %{HTTP_HOST} ^(.*)?companies/Inova-Solutions-Inc/Network-Clocks-by-Inova-Solutions/53e4f5e048e43c1d5cef656d [NC]
RewriteRule ^(.*)$ http://www.cmdgroup.com/companies/1142841/inova-solutions-inc/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*)?community.cmdgroup.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(.*)?community.reedconstructiondata.com [NC]
RewriteRule ^(.*)$ http://www.cmdgroup.com/community/ [R=301,L]
It is because your first RewriteCond %{HTTP_HOST} is wrong since you only match domain name using %{HTTP_HOST} variable. Change that rule to this:
RewriteRule companies/Inova-Solutions-Inc/Network-Clocks-by-Inova-Solutions/53e4f5e048e43c1d5cef656d$ http://www.cmdgroup.com/companies/1142841/inova-solutions-inc/ [R=301,L]
RewriteCond %{HTTP_HOST} community\.(cmdgroup|reedconstructiondata)\.com$ [NC]
RewriteRule ^ http://www.cmdgroup.com/community/ [R=301,L]

htaccess redirect from www to non-www

I am new to this.
Code from my .htaccess file goes like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
Redirect 301 /abc/ /abcnew/
I want this to redirect from www to non-www i.e., from http://www.example.com to http://example.com
I copied:
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
this code from here Generic htaccess redirect www to non-www.
I also checked in /etc/apache2/mods-enabled folder on my linux server. There "rewrite.load" this module is present.(I think this might mean that rewrite is enabled on my server, but correct me if I am wrong.)
Redirect 301 /abc/ /abcnew/
and just FYI this above code works fine(its redirecting my old links to new links).
I also tried this.
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Doesn't work for me.
Please help. Thanks in advance...
Edit:
this link I found this. But not sure what should be edited. Can anyone please point out.?
You need to place external (full) redirect rules before internal rewrite ones and also make sure to use mod_rewrite rules only.
Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
RewriteRule ^abc/?$ /abcnew/ [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

rewrite url to non www multiple sites

i have some sites they are running with the same files but different templates, my goal is to force use all www or non www domains.
this is my code:
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteCond %{HTTP:Host} ^[^.]+\.[a-z]{2,5}$ [NC]
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]
get it from other question on stackoverflow :)
But if I use www domain all is ok, if not get this redirect
http://domain.de/www/htdocs/user/_production/http%28?s%29://www.
Why not simplify your rules like this:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]