Redirect https and http www to https non www - apache

I want to rewrite the following
http://example.com
http://www.example.com
https://www.example.com
to
https://example.com
tried using
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
It doesn't seems to be working

I'd say the easiest thing to do is just write two rules:
# First make sure HTTPS is being used
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Then make sure www isn't being used
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

You can use:
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]

Related

Combining Rules in .htaccess file?

I'm wondering how to connect these two rewrite rules in one .htaccess file. Currently, I force https connections to my website with this rule:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{ENV:HTTPS} !=on
RewriteCond %{HTTP_HOST} !=SERVER_NAME
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Now I want to redirect from my www-subdomain to my real domain. There is a high rated answere here:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
But I fail to put these two together.
You can use:
RewriteEngine On
# if with www, redirect to https domain without www
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
# if http, redirect to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
If you test first for www and redirect to https, no need to test for http://www.
Or like #PanamaJack said, you just can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://domain.com%{REQUEST_URI} [NE,L,R=301]
But you need to change the domain name.

Redirect https to non http and Http to Https in directory with htaccess

I want to make my homepage to redirect to non https if it https
if https://example.com will redirect to http://example.com
and i want to make directory with ssl (just directory using SSL)
if http://example.com/directory wil redirect to https://example.com/directory
I'm use this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
but it just show loop
RewriteEngine On
RewriteCond %{HTTPS} ^on$
RewriteCond %{REQUEST_URI} !^/dir
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} ^off$
RewriteCond %{REQUEST_URI} ^/dir
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

redirecting https://domain.com to https://www.domain.com

I've recently moved my site to HTTPS and im trying to force all http traffic to https and also to the www. version
i.e forcing
https://domain.com
to
https://www.domain.com
I have this in my htaccess and all http to https works, apart from the redirect from https://domain.com to https://www.domain.com
What am i doing wrong?
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You need an OR condition here:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
with the above rule https://example.com was not redirecting to https://www.example.com.
For that to happen I have added the following rule too in the apache ssl vhost block
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.exmple.com%{REQUEST_URI} [R=301,L,NE]

Ignore No Subdomain In htaccess RewriteCond

I've got htaccess set up that redirects all subdomains except www to https. However I'd also like it to ignore http://domain.com - currently it redirects to https://domain.com which is no good for what I'm doing.
Any idea what the rewrite condition is for that?
I've tried:
RewriteCond %{HTTP_HOST} !^domain.com [NC]
RewriteCond %{HTTP_HOST} !^http://domain.com [NC]
But neither of those work.
Here's what I'm using at the minute.
# Redirect subdomains to https
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^domain.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I think your problem is that you need to escape the dot in domain.com, so domain\.com.
# Redirect subdomains to https
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^domain\.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You used a 301 (permanent) and not a 302, so you may have your browser not even trying to send requests to the http domain until you close. You should use 302 while testing and put the 301 only wen it's ok.
Try this:
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

mod_rewrite on substring in URL

I'm forcing HTTPS on my site using mod_rewrite but I want to change this to HTTP for any URL with the substring com_bookmangement in the URL.
So
http://www.example.com/index.php?option=com_content&view=article&id=85&Itemid=140
will be directed to
https://www.example.com/index.php??option=com_content&view=article&id=85&Itemid=140
BUT
https://www.example.com/index.php?option=com_bookmanagement&controller=localbooks&Itemid=216
will be directed to
http://www.example.com/index.php?option=com_bookmanagement&controller=localbooks&Itemid=216
I've tried this without success:
#rewrite everything apart from com_bookmanagement to HTTPS
RewriteCond %{SERVER_PORT} !443
RewriteCond %{REQUEST_URI} !com_bookmanagement=
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
#rewrite vouchers views to http
#RewriteCond %{server_port} 443
RewriteCond %{REQUEST_URI} ^com_bookmanagement=
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]
Any ideas?
If you use ^com_bookmanagement= it will only match if com_bookmanagement= appears at the start of the string. Try it without the ^ and =. Or what I would use:
#rewrite everything apart from com_bookmanagement to HTTPS
RewriteCond %{HTTPS} !=on
RewriteCond %{QUERY_STRING} !(^|&)option=com_bookmanagement(&|$)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#rewrite vouchers views to http
RewriteCond %{HTTPS} !=off
RewriteCond %{QUERY_STRING} (^|&)option=com_bookmanagement(&|$)
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Condition on QUERY_STRING instead of REQUEST_URI:
RewriteCond %{SERVER_PORT} !443
RewriteCond %{QUERY_STRING} !com_bookmanagement
RewriteRule ^(.*)$ https://www.mysite.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} com_bookmanagement
RewriteRule ^/(.*)$ http://www.mysite.com/$1 [R=301,L]
More on rewriting URIs with query strings here.