Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • Google+
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
Web Servers ›
Redirect http to https using htaccess

Redirect http to https using htaccess

November 23, 2010 Web Servers Jesin A Leave a Comment

Redirect your website from http://www.example.com to https://www.example.com using htaccess file in your Apache web server. It is crucial to collect financial and confidential information from a person through HTTP over SSL to make sure the transactions are secure. A customer cannot be asked to type https  in front of the URL each your website is visited. So you can make sure they are automatically redirected to the HTTPS version of the website each time they visit the URL.

Redirect http to https using htaccess

Before going ahead make sure you have SSL properly configured in your server with the necessary certificates in their place. Type https://www.example.com and make sure your website loads. Then ensure your Apache web server supports mod rewrite. Now create a file named .htaccess inside the document root (www or htdocs or public_html folder) if it already exists just edit it. Add the following lines inside the .htaccess file. Replace example.com with your domain name
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

What actually happens here is the rewrite engine checks at which port the request was made by the visitor, if it was made on port 80 (the default port for Web Servers) it is redirected to https://www.example.com/ The $1 at the end of the URL is used for a wild card redirect i.e. all files and folders accessed under example.com/ will be redirected to the https page. The 301 is the web server response code which says the page has been “Moved Permanently” to another location.

Note:- Make sure you enter the URL as it was signed by the certificate authority. If you bought an SSL certificate for example.com you cannot use it for www.example.com and vice versa. If you do use it the web browser will display an error.

Related posts:

Default ThumbnailUsing the Apache Redirect directive Default ThumbnailRedirect non www to www Default ThumbnailChange the default Directory Index in Apache Server php category thumbnailPHP error_log .htaccess Default ThumbnailHow to create custom error page in Apache

Tags: htaccess, redirect

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.