Redirecting in website using .htaccess - .htaccess

Can any one please help me how redirect all http://example.com and http://www.example.com to https://www.example.com. I have recently purchased an ssl for my site and hence needed it to be redirected. Redirecting using .htaccess will be more useful for me. Thanks in advance.

There are a lot of solutions out there. Here is a link to the apache wiki which deals with this issue directly.
http://wiki.apache.org/httpd/RewriteHTTPToHTTPS
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*)$ https://www.example.com/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e. http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context

Related

301 Direct to new Domain on https:// not https://www

I am looking to 301 an entire domain on https://www.olddomain.co.uk to a new domain on https://newdomain.com via .htaccess. Currently, the version I have will only work if you manually remove the www from the old link showing in Google SERP for the old domain listing - so it shows 'This site can’t be reached' when clicked. Removing the 'www' part of the URL and hitting return performs the redirect.
Current .htacess is:-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ https://newdomain.com/$1 [R=301,L]
</IfModule>
And I've also tried:-
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.co.uk [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.co.uk [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC]
Important to keep the SEO value form the old domain. Any assistance appreciated here.
Thanks in advance
Glennboy
Fixed in 2 parts:-
The DNS settings on the domain did not cover 'www' which was a simple error that would have stopped 'www' showing anyway.
Added a valid SSL Certificate to the old domain to ensure safe traversing of SSL - SSL
In short, the issue was not the .htaccess implementation which was perfectly valid and now working fine.

htaccess to remove the start of a domain

I have a domain that needs to be redirected in order to use a shared SSL certificate. I can redirect the domain no problem, the issue I have is the way this leaves the URL formatting i.e.:
http://www.example.com is redirecting to https://myhost.secure.com/www.example.com
I'm looking to hide the "myhost.secure.com/" part using htaccess but can't find a valid way to do it (I'm a complete novice, sorry). Redirect is being done via htaccess using code below:
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:https} !=on
RewriteRule ^(.*) https://myhost.secure.com/www.example.com/$1 [R,L]
How can I modify this to only display my domain and not the full redirected URL? The redirect works correctly, I see the site as it sits in the secure area of my FTP, but I don't want visitors to see the hosts redirect in the URL.

.htaccess not redirecting web site to https

I am running Apache/2.2.15 on Centos 6.6 and am using a free certificate from StartCom. My home page file is /var/www/index.php so I create a file /var/www/.htaccess with the following content, as suggested here.
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e. http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context
However, entering
myWebSite.com
in the URL box brings up my site in http protocol. If I enter
https://myWebSite.com
instead, I get my site in https protocol. My goal is to get my site in https protocol by simply entering
myWebSite.com
and I cannot see why the .htaccess file is not effecting that.
It doesn't appear that your .htaccess file is being read. So make sure you have AllowOverride All in your config.
Also for your rules, I wouldn't use SERVER_NAME, that isn't always set and sometimes is not correct. I would either use HTTP_HOST variable or your actual domain name. You also should specificy 301 for your redirect because without it 302 is default. You want this to be a permanent redirect.
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !^on [OR]
# This checks to make sure the connection is not already HTTPS
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^/?(.*)$ https://example.com/$1 [R=301,L]
I also made it where it would remove the www since you don't show your are using it.

How to Re-Direct non WWW, non HTTPS to WWW, HTTPS without Multiple Re-Directs?

I hope you can help with this htaccess issue please? I basically have the htaccess rules working apart from in one scenario. This is the case where a user visits the site on a non HTTPS and non WWW links.
Re-Directs
User visits on http/non-WWW URL
User is redirected initially to the http/WWW URL
User is then redirected to https://www.website
You can see the behaviour here:
http://childrens-curtains.co.uk
All other scenarios work fine.
I want to try to remove the 2nd redirect from the sequence so it behaves like this:
User visits on http and is immediately redirected to the https/www version without the 2nd step (if that makes sense?)
The current scenario causes multiple re-directs, which I understand is a bad approach from an SEO perspective.
This is my htaccess redirect rule:
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [L,R=301]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context
You can try this code keepin into .htaccess file.
To add www add following code.
RewriteCond %{HTTP_HOST} ^**domainname**\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$
RewriteRule ^index\.html$ "http\:\/\/www\.domainname\.com\/" [R=301,L]
To add http add following code.
RewriteCond %{HTTP_HOST} ^domainname.com [NC]
RewriteRule ^(.*)$ http://www.domainname.com/$1 [L,R=301]
Which will add both http as well as www to URL.
Thanks.

How to 301 redirect an entire domain while preserving the path

I am redirecting one domain to another, but I want to preserve the path in the redirect. So for example, I want to visit www.example.com/services/education/page.html, but my redirect will bring them to www.new-example.com/services/education/page.html. What do I write in my .htaccess file to preserve the path "/services/education/page.html"?
Right now I have:
redirect 301 http://www.example.com/ http://www.new-example.com/
But I'm not sure if that works or not (Can't test yet as I am waiting for domain details etc). I just want to be sure when I put the site live. Is that right or am I way off base?
Thanks!
This should do it:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ http://new-example.com/$1 [L,R=301]
try adding the following to your .htaccess in the root of your example.com domain
RewriteEngine On
RewriteBase /
#for all requests to www.example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com$
#redirect them to new-example
RewriteRule (.*) http://www.new-example.com/$1 [R=301,L]
Your original command uses the mod_alias Apache module, and it would work, though you may want to update it to:
Redirect 301 / http://www.new-example.com/
Removing the exact domain of the current (old) domain means all domains that point to that folder will be sent to the new domain, making that one-line script more robust.
The other answers use the mod_rewrite Apache module. If you have that also installed, that's fine to use, though it's 4+ lines of code compared to one. Additionally, mod_alias is part of the "base" package, so should be on all Apache servers, while mod_rewrite is an optional extension, so some might not have it.

Resources