.htaccess simple URL rewriting tutorial with SSL redirection

I'm going to share the most important article for web beginners name as .htaccess simple URL rewriting tutorial, rewriting can be useful for improving usability in the search engines of your content and serving clean URL looks good and attract more visitors. In the online web, you can easily get many tutorials with lot's of details, but may it's too much complicated for you as I assumed for me. So I made it simple as I can after analyzing most common needs.

.htaccess simple URL rewriting tutorial

Why .htaccess and what is this, why I'm talking about ?, yes I understand because it's common question so let me explain first-

What is .htaccess ?

The .htaccess a configuration file stored on a server. It contains Rules and handles the request as per specific rules (if any) and locate in server's ROOT directory. Especially here I'm talking about Linux servers.

htaccess simple URL rewriting tutorial with custom redirection

Important parameters for .htaccess : .htaccess having lot's of parameters and many usages, but here I only share most useful parameters because I don't think share all info usefull for first use and if this turorial can helps you to clear your concept then seriouly no use for too much info.

RewriteRule - Handle Apache for a specific single rewrite rule.

Explanation for Apache .htaccess characters: Most commonly using characters in .htaccess as below:

  • . (for any character)
  • + (one or more of the preceding)
  • {} (minimum to maximum quantifier)
  • ? (ungreedy modifier)
  • * (zero of more of the preceding)
  • ! (at start of string means "negative pattern")
  • ^ (start of string, or "negative" if at the start of a range)
  • $ (end of string)
  • [] (match any of contents)
  • - (range if used between square brackets)
  • () (group, backreferenced group)
  • | (alternative, or)
  • \ (the escape character itself)

.htaccess Flags: Flags takes place in end of rules, there are several as below:

C (chained with next rule)
CO=cookie (set specified cookie)
E=var:value (set environment variable var to value)
F (forbidden - sends a 403 header to the user)
G (gone - no longer exists)
H=handler (set handler)
L (last - stop processing rules)
N (next - continue processing rules)
NC (case insensitive)
NE (do not escape special URL characters in output)
NS (ignore this rule if the request is a subrequest)
P (proxy - i.e., apache should grab the remote content specified in the substitution section and return it)
PT (pass through - use when processing URLs with additional handlers, e.g., mod_alias)
R (temporary redirect to new URL)
R=301 (permanent redirect to new URL)
QSA (append query string from request to substituted URL)
S=x (skip next x rules)
T=mime-type (force specified mime type)

Creating a .htaccess file: It's simple and quick, open any editor create a new file and save as .htaccess extension without any name.

Ex- 

.htaccess (correct)

htaccess.txt (incorrect)

Simple URL rewriting and hiding extension-

Let's assume you have a website with contact URL such as:

yourname.com/contact.php 

or 

yourname.com/contact.html

Your site running well and everything fine with your website , but if I suggest you to for a clean URL like below:

yourname.com/contact

Looks great ? , here I hide URL extension and it's not only clean also have some pro benefits as-

Hide URL Extension & It's Benefits -

  • Your site URL looks clean and seems like pro web portals.
  • Google search algorithms love clean URL means it increases search engine ranking for your website.
  • Hide technology behind your website for normal internet users.

Insert below lines in yours .htaccess file:

RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

You can change .php to .html or anything based on your website technology.

Now remove .php or .html from your anchor link, for example in you nav menu

<li> <a href="../contact.php"> Contact Us </a>

to

<li> <a href="../contact"> Contact Us </a>

.htaccess configuration auto consider .php while running on server.

Set 404 error page using .htaccess

Setting up 404 error improve your website security from online attacks also helps to increase website ranking in search engines and important for professional sites.

Insert below code in .htaccess file:

ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php

Now create an error.php page, when URL was broken or not found you site will redirect on custom error.php

Redirect without www URL to WWW using .htaccess

I hate naked ( without www URL), if you are the same mind then a simple configuration will solve your problem-

RewriteEngine on
RewriteCond %{HTTP_HOST} ^tricksway\.com [NC]
RewriteRule (.*) http://www.yousite.com/$1 [R=301,L]

I'm using R=301 , I already explained about above flag. It will permanent redirect your naked URL to www but making such changes affect your search engine ranking temporarily.

HTTP to HTTPS, SSL redirection using .htaccess

You should have active SSL certificate or cloud flare setting before applying this rule. If you don't have SSL certificate for your site then you can get it FREE, read this - Enable Cloudflare SSL Free

If already have, copy below lines and paste in your .htaccess file:

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

I think above tutorial solve almost all your URL rewriting needs, if yes let us know, if not let us know :). Thanks for reading.

Himanshu is a young engineer living in India. Currently working at Cognizant as a Senior Engineer. He is an ethical hacker & blogger too, doing lots of crazy stuff... If you seem interesting, go through his portfolio: www.himstar.info : "Open Source. Millions of open minds can't be wrong!

Leave a reply:

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Site Footer

Sliding Sidebar

We are India’s largest Startup Community


We are team of ' Delhi Startups ' , most active startup community with strict spam policy.
We are making !deas happen..for future, business and jobs without charging anything, with connecting entrepreneurs.. It's a reason to trust on us.
Come and join or subscribe, we will defiantly give a reason to like us.

Our Facebook Page