Forcing SSL and WWW using .htaccess for a subdomain - .htaccess

I have hosted opencart website on subdomain beta.mydomain.com with ssl cirtificate installed.
I want to force https and www . when someone types beta.example.com should take them to https://www.beta.example.com
I tired following .htaccess entries
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example.com\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteBase /
I dont understand whats going wrong here. thanks in advance.

Well, the domain you're using in your example is not beta.example.com it's example.com you should change it to the domain you require. Something like:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.beta\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.beta.example.com/$1 [L,R=301]
RewriteBase /
What do you use the RewriteBase for?
I personally like to use the following process:
Redirect to www
Redirect to https
Something like:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
In case you haven't already checked it, this GitHub Repository has some useful info on .htaccess.

The easiest way is to update your config.php file in your home directory.
I don't have it in front of me, but you'd want to change your HTTP and HTTPS url to "https://www.beta.example.com".

for htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ https://www.yoursite.com%{REQUEST_URI} [NS,R,L]
alternative htaccess code
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
for php redirect
Open index.php file
find
<?php
add after
if (substr($_SERVER['HTTP_HOST'], 0, 4) === 'www.') {
header('Location: http'.(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 's':'').'://' . substr($_SERVER['HTTP_HOST'], 4).$_SERVER['REQUEST_URI']);
exit;
}

Related

Adding redirect rule to htaccess file

I have several domains pointed to my hosting. Suppose I have ex1.com, ex2.com, ex3.com pointed to my hosting. Now ex2.com is hosted on another hosting. Now I want to add a redirect rule on the htaccess file so that I can redirect ex1.com to ex2.com. What will be the code? I tried the code below but no luck :-
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ex1.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.ex1.com [NC]
RewriteRule ^(.*)$ http://www.ex2.com/$1 [L,R=301,NC]
Can anyone please inform me what I have to do?
You require Redirect 301:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]
This is useful when you use www.newdomain.com as your new domain name. If the domain is different, then use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
</IfModule>
Obviously replace olddomain and newdomain with the correct domains.

Remove wildcard subdomains but not existing ones with .htaccess

Hello!
I'm trying to set up my .htaccess file for wildcard subdomains, but I really have no clue how to do that.
I have "domain2" pointing to "domain1" as an alias, which is working perfectly, this is the code I'm using:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteRule ^(.*)$ http://%2.%3.%4/$1 [R=301,QSA,L]
RewriteCond %{HTTP_HOST} ^(.*\.?)domain2.co\.cc$ [NC]
RewriteRule (.*) http://%1domain1.co.cc/$1 [R=301,L]
I found the www redirect here btw: Optimize htaccess Wildcard Subdomain Code
Now, what I want is all non-existent subdomains to get removed and the ones that exist (like "blog.domain1.co.cc" to stay.
I hope someone can help me with this. Thanks!
RewriteEngine On
#no longer needed
#RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#don't redirect blog.example.com, forum.example.com and example.com
RewriteCond %{HTTP_HOST} ^((blog|forum)\.)?example\.com$
RewriteRule .* - [L]
#redirect the rest (including www.) to example.com
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Try adding the following to your htaccess file.
#if these lines already exist, skip them
RewriteEngine On
RewriteBase /
#if its not www or discussions subdomain
RewriteCond %{HTTP_HOST} !^(www|discussions)\.domain1\.co\.cc$ [NC]
#redirect to www domain
RewriteRule .* http://www.domain1.co.cc%{REQUEST_URI} [R=301,L]
For the following question
redirects subdomains and subdirectories to the other domain, just like that: forum.old.com/thread/12038213 --> forum.new.com/thread/12038213
Try
RewriteEngine On
RewriteBase /
#if domain is old.com
RewriteCond %{HTTP_HOST} ^(.+)\.old\.com$ [NC]
#redirect to new.com
RewriteRule .* http://%1.new.com%{REQUEST_URI} [L,R=301]

Non WWW to WWW redirection using htaccess File

I am using Codeigniter-.
My domain currently does not redirect to www version.
For example if I type mydomain.com then it stays mydomain.com. I want to redirect it to www.mydomain.com.
If someone types mydomain.com/controller/method then it should be www.mydomain.com/controller/method.
Another problem: I already tried other solutions but the problem is when it redirects to www version, it automatically adds "index.php" in the URL. But when I type www in the domain name then it works fine, no "index.php" in the URL. This problem occurs only during the redirection.
Here is my .htaccess file (I've removed the redirection code)
RewriteCond $1 !^(index\.php|system|rpc_relay.html|canvas.html|robots\.txt)
RewriteCond $1 !^(sitemap\.xml|export)
RewriteRule ^(.*)$ /index.php/$1 [L]
Any help would be greatly appreciated.
To redirect from http:// to http://www. , and also remove the route file (index.php) in the url, put these lines on your htaccess :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond $1 !^(index\.php|images|css|js|styles|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
the domain is :
domain.com
folder with direct access :
images|css|js|styles
hope this help
I've used the following before:
<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>
To redirect domain.com to www.domain.com, you could use the following rewrite rule. Please replace domain.com with your own domain name.
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
I don't know why there are such complex RewriteRules answers, even though Gobhi has provided a nice generic solution (= whatever the domain name is, it works).
Here's my solution.
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteRule (.*)/index\.php$ $1/ [QSA]
</IfModule>

.htaccess redirect subfolder to HTTPS

I'm trying to write a RewriteRule for my .htaccess file. Specifically, whenever a user accesses a specific subdirectory, I would like it to Rewrite to force an HTTPS connection.
For example, whenever someone accesses: http://www.mydomain.com/subdirectory (and any other sub-directories of that "subdirectory").
I'd like it to rewrite to https://www.mydomain.com/subdirectory
I've tried the following, but it appears to create a loop:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]
Also, this .htaccess file is placed in the root of my domain.
Any ideas on how to modify my RewriteRule?
Many Thanks!
I would put this into the domain's root directory:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(subdirectory/.*)$ https://www.mydomain.com/$1 [R=301,L]
This work for me, this allow you to redirect to https a specific folder, just add an htaccess file inside of the folder with the following content:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Ahh, its a combo of the two. MonoMano - you've omitted the subdirectory in the first part of the RewriteRule, therefore directing ALL traffic to the HTTPS subdomain address. I found that checking for 'off' was more reliable than checking for !=on, dont ask me why!
You'd want to add the subdirectory back in as per Floern's response, see below:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(subdirectory/.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]
Add the lines given below to .htaccess file of that subdirectory:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/subdirectory/$1 [L,R=301]
Use this if you don't want the address bar to show the subfolder. It will redirect yourdomain.com to yourdomain.com/subfolder but will look like you are still on yourdomain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?yourprimarydomain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
Try This
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
This will remove www prefix , and force https:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://example.com%{REQUEST_URI} [NE,R=301,L]
RewriteRule ^((?!subdirectory/).*)$ /subdirectory/$1 [L]
Change example.com for you main domain and subdirectory for Sub Directory
Try
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]

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