How to write RewriteCond and RewriteRule for changing url www to non-www and http to https? - redirect

In Apache's httpd.conf file, how to rewrite module for making url's www to non-www and force http to https?
When I tried below, it becomes infinite redirection error
RewriteCond %{HTTP_HOST} ^www.(.*)$
RewriteRule ^(.*)$ https://example.com$1
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ https://example.com$1
or
RewriteCond %{HTTP_HOST} ^www.(.*)$ [OR]
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ https://example.com$1

To redirect from www/http to non-www/https, you can use :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.com%{REQUEST_URI [NC,L,R]

Related

Redirect chain domain htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain.com.au [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http%1://domain.com.au/$1 [L,R,NE]
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^(.*)$ https://domain.com.au/$1 [R,L]
I am getting Redirect chain and errors, any help would be great
thank you
Redirect chain issues

How can I direct all visitors to https://www.example.com?

I thought I had it right but when I type just example.com in the browser it isn't redirecting, I get the no server IP error.
Code I have now is:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{http_host} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Cheers
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

Redirection With https from www to non-www

I have very simple requirement, I want a set of RewriteRule which can do the following things
Redirect all www request to non-www.
Redirect all request to https.
Ultimately my URL requirement is like http://domain.com
I am able to do most things but a request like https://www.domain.com is not redirecting.
This my redirection rule:
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*) http://domain.com/$1 [L,R=301]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Please suggest what I need to do.
Use this code below
RewriteEngine On
#Redirect HTTP with www to HTTPS without www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]
# Redirect HTTP without www to HTTPS without www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect HTTPS with www to HTTPS without www
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]

Permanent subdomain https:// redirection for Wildcard ssl certificate

I put inside the public_html/ .htaccess code (at the begining)
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]
and https:// was forced with success.
But i put on the public_html/subdomain/ .htaccess code (at the begining)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
and the subdomain get's messy.
What is wrong about this code i put in the subdomain?
I can't do anything more. Some expertise is needed.
Thank you.
Create your new .htaccess file with the following code in your sub folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^demos.9lessons.info$ RewriteCond
%{REQUEST_URI} !^/demos/ RewriteRule (.*) /demos/$1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^labs.9lessons.info$ RewriteCond
%{REQUEST_URI} !^/labs/ RewriteRule (.*) /labs/$1

Redirecting domain names to the same domain?

I want to redirect the following domain names :haloservers.net,haloservers.nl and servers.xgclan.com to the domain name www.haloservers.us
I tried putting the code below in the htaccesses file but this didn't work.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.?)haloservers.net$
RewriteRule ^(.*)$ http://www.haloservers.us/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.?)haloservers.nl$
RewriteRule ^(.*)$ http://www.haloservers.us/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(servers\.)xgclan.com$
RewriteRule ^(.*)$ http://www.haloservers.us/$1 [R=301,L]
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?haloservers\.net [NC]
RewriteRule ^(.*)\.html$ http://haloservers.us/$1 [R=301,L]
The following code fixed the problem.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)haloservers(.nl|.net)$
RewriteRule ^(.*) http://www.haloservers.us/$1 [L,R=301]
RewriteEngine On RewriteCond %{HTTP_HOST} ^(www\.)?haloservers(.nl|.net) [NC]
RewriteRule ^(.*)\.html$ http://haloservers.us/$1 [R=301,L]
I got my solution from col-dejong # http://gathering.tweakers.net/forum/list_messages/1497390/last