htaccess to add https to url except for 1 directory - .htaccess

#ADD HTTPS TO URL
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/hd/209$
RewriteRule ^(.*)$ https://domain.com/$1 [L,R=301]
I'm trying to force ssl for all the pages on the site except for /hd/209
I've tried the code above but it still forces that /hd/209 to ssl

I will add a redirect for folder
redirect 301 /folder/ https://domain.com/carpinteria-aluminio-mallorca/

Clear your browser cache and restart it.
Then replace your code with this:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule (?!^hd/209)^(.*)$ https://domain.com/$1 [L,NC,R=301]

Related

Forcing HTTPS to one directory - Removing WWW

I've got a slight problem, this is what i'm using to force https to the checkout directory, just a precaution but the problem is once you go back through to the main pages it inserts www into the URL.
This is a problem as the rest of the sight does not use a www, can anyone suggest how to addapt this to remove/ keep out the www
# force https for all URLs in /checkout
RewriteCond %{HTTPS} =off
RewriteRule ^x9 https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# force http for all other URLs that are not in /checkout
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/x9
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Edit
If you say it's not that section above. This problem only started after the above code was added though, Ive never had this problem before and the code below has been on my site for several months.
#Take off index.html
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC]
RewriteRule . http://www.%{HTTP_HOST}%1 [R=301,NE,L]
In that case keep your .htaccess like this:
#Take off index.html
RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%1 [R=301,L]
# force https for all URLs in /checkout
RewriteCond %{HTTPS} =off
RewriteRule ^x9 https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# force http for all other URLs that are not in /checkout
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/x9
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

.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]

htaccess -> need to redirect all www to non-www and one page only to ssl

I've looked "everywhere" but just can't seem to figure this one out.
I need the htaccess to do 2 things for me:
Force all www requests to non-www requests
Also force http (non-ssl) on all pages but one in which case I need ssl
Also, this is a wordpress site using permalinks, so I've got that chunk of htaccess code wordpress also puts in there. But somehow everything works fine, except for when I go to the page that needs to be forced to ssl, then it just redirects to the root of the site.
This is what my code looks like:
RewriteEngine On
RewriteBase /
# Redirect from www to non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301]
# Turn SSL on for secure-page
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^secure-page[/]?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Turn SSL off everything but secure-page
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^secure-page[/]?$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
So results are:
any www and non-www and http urls are redirected to non-www and http -> OK
any www and non-www and https urls (excl. secure-page) are redirected to non-www and http -> OK
secure-page url are redirected to the site root and I can't figure out why -> NOT OK
Can someone please help?
Thanks
RewriteEngine On
# Redirect from www to non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/secure-page[/]?$
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/secure-page[/]?$
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
# Turn SSL on for secure-page
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/secure-page[/]?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Turn SSL off everything but secure-page
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/secure-page[/]?$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
I've updated my answer!

Generic htaccess redirect www to non-www

I would like to redirect www.example.com to example.com. The following htaccess code makes this happen:
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
But, is there a way to do this in a generic fashion without hardcoding the domain name?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Same as Michael's except this one works :P
But if we need to do this for separate http and https:
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]
Redirect non-www to www (both: http + https)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
If you want to do this in the httpd.conf file, you can do it without mod_rewrite (and apparently it's better for performance).
<VirtualHost *>
ServerName www.example.com
Redirect 301 / http://example.com/
</VirtualHost>
I got that answer here: https://serverfault.com/questions/120488/redirect-url-within-apache-virtualhost/120507#120507
Here are the rules to redirect a www URL to no-www:
#########################
# redirect www to no-www
#########################
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
Here are the rules to redirect a no-www URL to www:
#########################
# redirect no-www to www
#########################
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
Note that I used NE flag to prevent apache from escaping the query string. Without this flag, apache will change the requested URL http://www.example.com/?foo%20bar to http://www.example.com/?foo%2250bar
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/(.*)$ https://%1/$1 [R]
The RewriteCond captures everything in the HTTP_HOST variable after the www. and saves it in %1.
The RewriteRule captures the URL without the leading / and saves it in $1.
Try this:
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ %{HTTP_HOST}$1 [C]
RewriteRule ^www\.(.*)$ http://$1 [L,R=301]
If the host starts with www, we stick the whole host onto the start of the URL, then take off the "www."
Complete Generic WWW handler, http/https
I didn't see a complete answer. I use this to handle WWW inclusion.
Generic. Doesn't require domain info.
Forces WWW on primary domain: www.domain.com
Removes WWW on subdomains: sub.domain.com
Preserves HTTP/HTTPS status.
Allows individual cookies for domain / sub-domains
Please let me know how this works or if I left a loophole.
RewriteEngine On
RewriteBase /
# Force WWW. when no subdomain in host
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ [NC]
RewriteCond %{HTTPS}s ^on(s)|off [NC]
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Remove WWW. when subdomain(s) in host
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|off [NC]
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)(.+\.)(.+\.)(.+)$ [NC]
RewriteRule ^ %1%3%4%5%{REQUEST_URI} [R=301,L]
There can be a lot of misinformation out there about htaccess redirects, I find. First off, make sure your site is running on Unix using Apache and not on a Windows host if you expect this code to work.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
(Make sure there are no line spaces between each line of text, though; I have added an extra space between lines so it renders okay in this window.)
This is one snippet of code that can be used to direct the www version of your site to the http:// version. There are other similar codes that can be used, too.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/subfolder/$1 [R=301,L]
For subfolder
www to non www with https
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
For those that need to able to access the entire site WITHOUT the 'www' prefix.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]
Mare sure you add this to the following file
/site/location/.htaccess
Using .htaccess to Redirect to www or non-www:
Simply put the following lines of code into your main, root .htaccess file.
In both cases, just change out domain.com to your own hostname.
Redirect to www
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.tld [NC]
RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]
</IfModule>
Redirect to non-www
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.tld [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]
</IfModule>
I used the above rule to fwd www to no www and it works fine for the homepage, however on the internal pages they are forwarding to /index.php
I found this other rule in my .htaccess file which is causing this but not sure what to do about it. Any suggestions would be great:
############################################
## always send 404 on missing files in these folders
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
############################################
## never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
############################################
## rewrite everything else to index.php
RewriteRule .* index.php [L]
RewriteEngine on
# if host value starts with "www."
RewriteCond %{HTTP_HOST} ^www\.
# redirect the request to "non-www"
RewriteRule ^ http://example.com%{REQUEST_URI} [NE,L,R]
If you want to remove www on both http and https , use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|offs
RewriteRule ^ http%1://example.com%{REQUEST_URI} [NE,L,R]
This redirects
Non ssl
http://www.example.com
to
http://example.com
And
SSL
https://www.example.com
to
https://example.com
on apache 2.4.* you can accomplish this using a Redirect with if directive,
<if "%{HTTP_HOST} =='www.example.com'">
Redirect / http://example.com/
</if>
use: Javascript / jQuery
// similar behavior as an HTTP redirect
window.location.replace("http://www.stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
Or .htaccess:
RewriteEngine On
RewriteBase /
Rewritecond %{HTTP_HOST} ^www\.yoursite\.com$ [NC]
RewriteRule ^(.*)$ https://yoursite.com/$1 [R=301,L]
and The PHP method:
$protocol = (#$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {
header('Location: '.$protocol.'www.'.$_SERVER ['HTTP_HOST'].'/'.$_SERVER['REQUEST_URI']);
exit;
}
Ajax
$.ajax({
type: "POST",
url: reqUrl,
data: reqBody,
dataType: "json",
success: function(data, textStatus) {
if (data.redirect) {
// data.redirect contains the string URL to redirect to
window.location.href = data.redirect;
}
else {
// data.form contains the HTML for the replacement form
$("#myform").replaceWith(data.form);
}
}
});
The only way I got it to work...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^site\.ro
RewriteRule (.*) http://www.site.ro/$1 [R=301,L]
If you are forcing www. in url or forcing ssl prototcol, then try to use possible variations in htaccess file, such as:
RewriteEngine On
RewriteBase /
### Force WWW ###
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
## Force SSL ###
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
## Block IP's ###
Order Deny,Allow
Deny from 256.251.0.139
Deny from 199.127.0.259
This is updated to work on Apache 2.4:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
The only change vs Michael's is to remove the [NC], which produces the "AH00665" error:
NoCase option for non-regex pattern '-f' is not supported and will be ignored
The selected answer and many other solutions here dropped the the part of the url after /, so basically it always redirected to main domain, at least for me.. So i am adding working sample respecting full path after slash..
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [L,R=301]
Alternative approach if .htaccess customization is not ideal option:
I've created simple redirect server for public use. Just add A or CNAME record:
CNAME r.simpleredirect.net
A 89.221.218.22
More info: https://simpleredirect.net
I am not sure why u want to remove www.
But reverse version would be:
# non-www.* -> www.*, if subdomain exist, wont work
RewriteCond %{HTTP_HOST} ^whattimein\.com
RewriteRule ^(.*)$ http://www.whattimein.com/$1 [R=permanent,L]
And advantage of this script is:
if u have something like test.whattimein.com or any other (enviroments for developing/testing)
it wont redirect U to the original enviroment.
Added if localhost, ignore redirection (for development purpose in local environment). If not localhost AND (not https OR it’s www), redirect to https and non-www.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !localhost [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Add
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
to your .htaccess before any other rule.
Hi you can use following rules on your htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Resources