April 15, 2021 2 Comments

Last Updated on April 23, 2021 by amitoverflow1

    htaccess tutorial for beginners

    htaccess has always been a confusing topic for all Apache users especially for newbies who are learning to write RewriteRules to modify URLs via an htaccess file. The reason why some people find it confusing is because the Apache mod-rewrite documentation is hard to follow and there are no easy to follow htaccess step by step tutorial available on the internet.
    I often see users struggling with their RewriteRule code on StackOverflow and I help them fix issues when I can.

    In this 10 minute read article I am going to post an htaccess short step by step tutorial for people who wish to learn some basics of URL rewriting on Apache server.
    If you follow this tutorial from top to the bottom then I am sure you will learn alot about URL rewriting and you will have some basic idea to create your own RewriteRules.


    Table of contents


    What is an htaccess file?

    It’s a directory level configuration file used by Apache web server to modify URLs , directory appearance and other server settings.

    .htaccess is a dot prefix file.
    The name starts with a single dot . and ends with htaccess .

    You can change the name to make it look something like htaccess.txt from your server.config file.
    The benefit of using an htaccess file is that it’s easy to edit and available on all types of hosting services.

    The downside of using an htaccess file is that it slows down your server performance but that’s not going to be a big issue for you.
    You can see the world’s largest CMS WordPress also uses a htaccess file to shorten the blog URLs.

    Apache mod-rewrite

    Rewrite module aka mod-rewrite is a powerful Apache module that provides URLs rewriting functionalitis to Apache users.
    Mod-rewrite provides URL rewriting directives such as

    RewriteEngine
    RewriteBase
    RewriteRule
    RewriteCond
    All these directives are used in a htaccess file to manipulate incoming URL requests.

    You will learn more about these directives later in this article but for now just focus on what the Rewrite module is and how to enable it.
    Almost all live web hosting providers will provide you Apache server with pre-installed mod-rewrite. But in some rearest cases if it’s not already installed then you can ask your service provider to enable it for you or you can also enable it yourself changing the line AllowOverride none to AllowOverride all this will then make it possible to use mod-rewrite in an htaccess file.

    To verify whether your server supports mod-rewrite in htaccess , you can do the following :

    • Create an .htaccess in your public_html folder.
    • Put some random texts to your htaccess file like “fooooobarrr” .
    • Now, visit any URL on your site.

    If it generates a 500 internal server error then the mod-rewrite is enabled on your server otherwise if you don’t get any error then you will need to enable this .

    Let’s create our first Hello-world RewriteRule

    If you are learning to write htaccess rules while following this article , then I suggest you to start learning with an empty htaccess file.
    Create an htaccess file on your document root and put the following contents in that file :

    RewriteEngine on
    RewriteRule ^helloWorld$ /index.php
    

    Save the htaccess file and type the following URL into your browser address bar :
    https://yourdomain.com/helloWorld
    You will see the real rewrite mazic happening now.
    The rule will internally map your URL path /helloWorld to the destination file /index.php.
    The redirection happens behind the screen. You will see contents of the index.php file on the /helloWorld path.

    Congratulations , you have written your first helloWorld rule successfully.
    Now let’s understand how the code above works. Let’s understand it line by line.

    • RewriteEngine on : This directive turns on the engine for rewriting URLs.
      This directive should be placed once at the top of your htaccess.
    • RewriteRule ^helloWorld$ /index.php : This is a RewriteRule directive provided by mod-rewrite. This line rewrites the request from /helloWorld to /index.php .
      You might have noticed one thing that our Rule’s pattern doesn’t start with a leading slash / .
      This is because RewriteRule uses a relative path in its pattern so ^/helloWorld$ will not work in htaccess but in a server.config file.

    Okay, now I hope you have had some basic idea about how to create a simpe RewriteRule.
    Now we will learn about other parts of a RewriteRule.

    Mod-rewrite Flags

    Flags are optional in RewriteRule but you will need to use them in most cases.
    Flags provide additional instructions to a RewriteRule.
    For example a RewriteRule with [R] flag tells the rule to make an external redirection of URLs.

    See a list of most commonly used flags in mod-rewrite .

    Flags are used inside sequere brackets [] .
    You can use multiple flags with a RewriteRule seperating them by a comma char ,.

    Flags are optional in RewriteRule but you will most often need to use them in Rule to change the URL rewriting behaviour.

    You will needv to use [NC] flag in the following Rule:

    RewriteRule ^foobar$ /index.php

    NC stands for NoCase, this flag is used to match both uppercase and lowercase chars in URI.
    With NC, the rule above will match the following both URIs :


    /foobar

    and
    /fOObAr
    otherwise without this flag, by default RewriteRule only matches the case sensitive URI string.

    You can learn more about flags on this post .

    Conditions in RewriteRule

    You can use conditions with your RewriteRule to rewrite or redirect your site URLs based on some specific conditions.

    RewriteCond directive is used for this purpose.
    With RewriteCond you can rewrite your URL based on host , https , request URI headers.

    Check this post to learn more about using RewriteCond directive.
    You can use if/else logic for RewriteRule .

    The following Rule uses RewriteCond to redirect the URL based on host header. The condition used in this rule can also be read as
    “If host ==example.com then Redirect .

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example.com$
    RewriteRule ^foobar$ /index.php [R]
    

    This rule will redirect /foobar to /index.php if the condition is met.

    You can use multiple conditions with a single RewriteRule .

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.example.com$
    RewriteRule ^foobar$ /index.php [R]
    

    The conditions above uses [OR] flag. This makes one of the conditions optional. So you can read it as :
    “If host ==example.com OR host==www.example.com” then Redirect.

    To redirect http URLs to https , you need a condition logic. That checks if the incoming URL is using http scheme , then redirect the URL to use https :

    RewriteEngine on
    
    RewriteCond %{REQUEST_SCHEME} ^http$
    RewriteRule (.*) https://example.com/$1 [R]
    

    I hope now you have had some basic idea of how conditions work and how to use them with RewriteRule.

    Regex in mod-rewrite

    Mod-rewrite directives RewriteRule and RewriteCond use a regular expression based pattern that makes the match easier.
    With a Regex based pattern a single RewriteRule can match multiple URIs.

    If you want to learn Regex , you can follow this tutorial .

    The following rule with regex pattern can either match /helloWorld or /hello ladies :

    RewriteEngine on
    RewriteRule ^(helloWorld|hello ladies)$ /index.php
    

    The match is saved in $n variable . Since we have one capture group in the rule above , the match is saved in $1 .

    You can use the matched value in the destination string of RewriteRule.

    RewriteEngine on
    RewriteRule ^(helloWorld|hello ladies)$ /index.php?q=$1
    

    The rule above rewrites /helloWorld to /index.php?q=helloWorld

    You can use Regular expressions with RewriteCond :

    RewriteCond %{HTTP_HOST} ^(www\.)example.com$
    

    The condition above checks both www and non-www domain .

    Isn’t it easier with Regex?

    RewriteRule code examples

    WWW to non-www

    With the following rule you can redirect your www subdomain to root domain :

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

    Enforce SSL

    You can redirect your insecure traffic (http) to the secure version (https) using this rule :

    RewriteEngine on
    RewriteCond %{HTTPS} off [NC]
    RewriteRule (.*) https://example.com/$1 [R=301,L]
    

    Add a trailing slash

    To add a trailing slash to your URLs , you can use the following :

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule !/$ %{REQUEST_URI}/ [R,L]
    

    Allow non-php extension

    If you want to access your .PHP files without using extension at the end , you can use :

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME}.php -f
    RwriteRule ^(.+?)/?$ /$1.php [L]
    

    More rules : 5 Awasome htaccess rules you can just copy & paste .

    I hope this tutorial was helpful. If there is anything that you couldn’t understand or you need my help with htaccess , you can post a comment bellow.

    Thank you so much reading this.

2 thoughts on “Learn the basics of mod-rewrite | htaccess tutorial step by step for beginners”

Leave a Reply

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