htaccess exclude specific folder - .htaccess

for a website I need a redirect from
domain without www TO domain with www
and
http TO https
The final URL has to be always https://www.myshop.com ...
I solved this successfully with this commands:
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
The problem ist that I have to exclude a specific folder.
The URL to exclude is: http://www.mydomain.de/myfolder/filename.php
This means: If this URL is requestet there mustn't be a redirect.
Other tutorials I found did not work, sorry :-(
Thanks for any help and best regards...

You can have a skip redirect rule before these 2 redirect rules you have:
RewriteEngine On
# skip myfolder/filename.php from any redirects below
RewriteCond %{THE_REQUEST} /myfolder/filename\.php[?/\s] [NC]
RewriteRule ^ - [L]
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Maybe something like that
RewriteEngine on
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteRule ^http://www.mydomain.de/myfolder/filename.php$ - [L]
RewriteRule ^.*\filename.php$ http://www.mydomain.de/myfolder/filename.php [R=301,L]

Adding something like this as a first ruleset may help. (May want to revisit those [L,R] options though.
# check if myfolder is contained
RewriteCond %{REQUEST_URI} ^[^/]*/myfolder(/.*)?$
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Related

.htaccess redirect from non-www http subfolder to www https subfolder

I use this simple code to redirect non-www http to www https
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
It succesfully redirect http://example.com to https://www.example.com.
But when I tried to access http://example.com/somefolder it did not redirected. Am I missing some syntax here? or something else?
Change %{REQUEST_URI} to /$1 ( first match in rule .* )
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [L,R=301]
or modify last rule pattern:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Http with .html to https without html in one redirect

I'm trying rewrite our website's htaccess file. I'm not too good with htaccess but I'm studying our existing .htaccess file, right now what I understand is that we have htaccess directives that redirects http to https.
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
And another right below that redirects from a request having a .html extension into no .html.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
I'm not sure, but does R=301 mean redirect?
If so, is it doing a redirect after each match in the rewritecond?
And is there a way that I can create another directive that matches both conditions before trying to redirect to both rules?
You can use a single redirect rule for both 301 redirects:
RewriteEngine On
RewriteCond %{THE_REQUEST} \.html\s [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.+?)(?:\.html)?$ https://%{HTTP_HOST}/$1 [R=301,NC,NE,L]
You can use:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,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]

Redirect to HTTP non-www to HTTPS www htaccess

I want to redirect from any direction to our site with HTTPS protocol, but some redirects it's not working. I want this:
http://www.site.co TO https://www.site.co
http://site.co TO https://www.site.co
This is my htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
The second rule it's not working. It going to another direction inside our site, and it isn't redirect to HTTPS site.
Try it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
The only real difference here is that first we redirect from non-WWW to WWW then we check for HTTPS and redirect it.
If it does not work, try this one:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
The answer by Prix works. To make it more dynamic lets use SERVER_NAME and REQUEST_URI instead of a static domain name.
RewriteEngine On
#we replace domain.com/$1 with %{SERVER_NAME}%{REQUEST_URI}.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
#here we dont use www as non www was already redirected to www.
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
Sorry I don't have enough point to comment, but the rule of #prix will bring unneeded redirect.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
You can try http://domain.com/ on GTMETRIX and you will get this message
"Avoid landing page redirects for the following chain of redirected URLs."
http://domain.com/
http://www.domain.com/
https://www.domain.com/
To prevent that, go to the first RewriteRule and add a "s" at the end of http. The new set of rule will look like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
This is http to https and non www to www redirection using .htaccess
If you want to redirect to https and www you can use this code
http to https redirection:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
</IfModule>
Non www to www redirection:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
If you want to use booth functionality place the above all code respectively
In case you are using One.com as your webhost you should use the following code instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have an asp.net website and the host provider is not supporting IIS rewrite module i have tested all this methods and only this code gives woorank.com confirmation maybe useful for another .net developer :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]
This will use for both www or non-www
If you try to open link with www then url redirect to https with www
Example : http://domain.com redirect to https://domain.com
or If you try to open link with non-www then url redirect to https with non-www
Example : http://www.domain.com redirect to https://www.domain.com
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Just put the following in the .htaccess file
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [END,NE,R=permanent]
Try this methode:
# Redirect 301 all to https://www
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.example.com/$1 [L,R=301]
</IfModule>
Why sometimes you don't need code %{HTTP_HOST} and use your domain exact url with www for the last line, because usually the result will have two www like:
https://www.www.yourdomain.com
Btw if you use cloudflare, just create pagerules redirect 301 all to https and www version.
Create pagerules
Fill with
example.com/*
Choose setting forward url then choose 301 and fill with this
https://www.example.com/$1
Save it and all will be redirected to https and www version.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
For example https://example.com should be going to https://www.example.com
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ "https\:\/\/www\.example\.com\/$1" [R=301,L]
Note - you should change example.com to your own domain.

Using .htaccess to redirect www URLs to non-www for https

Please provide me with .htaccess syntax to do the following:
Redirect from http to https
Redirect from https://www.domain.com.uk to https://domain.com.uk
I tried the following but it didn't work:
RewriteCond %{HTTP_HOST} ^www.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]
Try adding the following to your htaccess file in the root folder of your domain.
RewriteEngine on
RewriteBase /
#if not domain.com.uk then redirect to domaim.com.uk
RewriteCond %{HTTP_HOST} !^domain\.com\.uk$ [NC]
RewriteRule .* http://domain.com.uk%{REQUEST_URI} [L,R=301]
#if not https
RewriteCond %{HTTPS} off
#redirect to https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.com.uk
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://domain.com.uk/$1 [R=301,L]
For me this work, just tested. So http://www.domain.com.uk into https://domain.com.uk .

Resources