htaccess rewritecond rewriterule http https - .htaccess

I have a website on apache with ssl. I want to redirect the users to the https version but also I need to be able to open the 'http' version when I need it.
For example:
example.com --> has to be redirected to 'https://www.example.com'
www.example.com --> has to be redirected to 'https://www.example.com'
http://example.com --> has to open 'http://example.com' without redirect
I'm trying with this but can't reach the goal:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^http:\\example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^http:\\example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
----------------------------------------------------------
RewriteCond %{HTTP_HOST} !^http\:\\$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
----------------------------------------------------------
I have tried a few more syntaxes..

Related

redirect https to http for domain.com page only by using htaccess

I have purchased SSL certificate. And i have added https to all my web page using below script in htaccess file.
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301,NC]
Here i need to redirect from https to http for my homepage only.
Example:
https://www.domain.com to http://www.domian.com
Other pages should be redirect like below.
http://domain.com/login to https://www.domain.com/login
http://www.domian.com/register to https://www.domian.com/register
Note:
I need to append www to all my URLs for SEO purpose.
Thanks in advance.
You can use .+ to not to match landing page in your regex:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.+)$ https://www.domain.com/$1 [L,R=301,NE]
However since you're also adding www I suggest following rules:
RewriteEngine On
# all pages except home page
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(?!frontpageajax).+$ https://www.%1%{REQUEST_URI} [L,NC,R=301,NE]
# landing page redirect https=>http
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(frontpageajax/?)?$ http://www.%1 [L,NC,R=301]

Redirect HTTP to HTTPS excluding one sup domain and add www if needed

I want to redirect my SSL site (domain.com) with HTTPS protocol but not domain.org which is actually a sub-directory of domain.com (domain.com/domainorg/). I want this:
http://domain.com TO https://www.domain.com
http://domain.org TO http://www.domain.org
I tested my htacess on http://htaccess.mwl.be/ and it works fine but when I actually run it, I get “The page isn't redirecting properly” and it appears to be in a loop when I test all options.
This is my htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www.)?domain.org$
RewriteRule ^/?(.*)$ http://www.domain.org/$1 [L,R=301]
RewriteRule ^/?(.*) https://www.domain.com/$1 [L,R=301]
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>
I am not proficient at htacess at all. What do I need to correct or change?
Why are some of your rules outside <IfModule mod_rewrite.c>? I'd put all of them inside it.
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirecting domain.com http URLs to https (any non-www will be rewritten to www as well)
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
# Redirecting domain.com non-www domain.com URLs to www (any non-https will be rewritten to https as well)
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
# Redirecting domain.org non-www URLs to www (any https will be rewritten to http as well)
RewriteCond %{HTTP_HOST} ^domain\.org$ [NC]
RewriteRule ^(.*)$ http://www.domain.org/$1 [R=301,L]
# Redirecting domain.org https URLs to http (any non-www will be rewritten to www as well)
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
RewriteRule ^(.*)$ http://www.domain.org/$1 [R=301,L]
</IfModule>
You may also force URLs like this domain.com/domainorg/* to be redirected to http://www.domain.org/* using:
RewriteCond RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond RewriteCond %{REQUEST_URI} ^\/?domainorg(\/.*)?$ [NC]
RewriteRule ^.*$ http://www.domain.org%1 [R=301,L]

301 redirect to new domain + redirect www to non-www

i have this .htaccess code for redirecting URL with www to URL without www:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Now, i would like to add code for redirect whole web to another domain via 301 http code. But I don't want to get redirect loop.
Could I use this code:
RewriteCond %{HTTP_HOST} !domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Thank You.

.htaccess: force www on all pages, force https on one page, force http on all other pages

I have this site, example.com. What I want to do is force www on all pages, force https on one page, force http on all other pages.
I am using the following .htaccess code to redirect all http traffic to www:
#Redirect to www
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
and the following to set SSL on my "form" page:
#Force SSL on a specific directory
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{REQUEST_URI} form
RewriteRule ^(.*)$ https://www.example.com/form/$1 [R,L]
Problem is, when I hit https://example.com/, it does not redirect to www (https://www.example.com), and especially not http and www (http://www.example.com) like I want it to. I tried this:
#Redirect SSL to www
RewriteCond %{ENV:HTTPS} on [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [R=permanent,L]
but that was a really dumb attempt and didn't work. How can I accomplish all three at once?
Try this:
# Handle nonSSL
RewriteCond %{HTTPS} on
RewriteRule !^/?form(/|$) http://www.ravicti.com%{REQUEST_URI} [L,R=301]
# Handle SSL
RewriteCond %{HTTPS} off
RewriteRule ^/?form(/|$) https://www.ravicti.com%{REQUEST_URI} [L,R=301]
# Add the missing www for both HTTP and HTTPS
RewriteCond %{HTTP_HOST} ^ravicti.com$ [NC]
RewriteCond %{HTTPS}:s (on:(s)|off:s)
RewriteRule (.*) http%2://www.ravicti.com%{REQUEST_URI} [L,R=301]

Redirect https and http in .htaccess

I have an old url (test.me.com) and I would like to redirect it to www.me.com.
So I added this line to my .htaccess file:
RewriteRule ^(.*)$ http://www\.me\.com/$1 [L,R=301]
But as it turns out, Google has already indexed some of the pages. And since I have an SSL Certificate, the full url is https://test.me.com. So the redirect of above won't affect the https files...
I've tried this one, but with no luck.
RewriteCond %{HTTPS} =on
RewriteRule ^(.+)$ - [env=ps:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.+)$ - [env=ps:http]
# redirect urls with index.html to folder
RewriteCond %{HTTP_HOST} ^test.me.com [NC]
RewriteRule ^.*$ %{ENV:ps}://www.me.com/%1 [R=302,L]
How can I configure my .htaccess file that both http://test.me.com and https://www.test.com are redirected to http://www.me.com?
# redirect https requests or request on test.me.com to http://www.me.com
RewriteCond %{HTTPS} =on [OR]
RewriteCond %{HTTP_HOST} ^test\.me\.com$ [NC]
RewriteRule ^(.*)$ http://www.me.com/$1 [R=301,L]
or alternatively to also redirect me.com to www.me.com
RewriteCond %{HTTPS} =on [OR]
RewriteCond %{HTTP_HOST} !^www\.me\.com$ [NC]
RewriteRule ^(.*)$ http://www.me.com/$1 [R=301,L]

Resources