January 25, 2014 7:26 pm

How to Rewrite urls with .htaccess

I have received many requests from my readers to write some thing on .htaccess so today I am going to explain you how to rewrite URLs with .htaccess. Hypertext  access is shortly called as htaccess, it rewrite URLs for directory in which file placed it manage that directory only, we can make SEO friendly URLs and boost your search engine ranking.

Rewrite URLs with .htaccess

Write your htaccess code inside these tags:

<IfModule mod_rewrite.c>
RewriteEngine on
#Write your conditions here#

Example Pretty URLs 




<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1

RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1

In your profile.php file you get user value in $_GET[‘user’] in both condition.

Remove file extension .php or .html




<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d # rewrite condition if it is not&nbsp;directory

RewriteCond %{REQUEST_FILENAME}\.php -f # rewrite condition if it file with php extension

RewriteRule ^(.*)$ $1.php

This example can remove .php extension and show you only home.

Redirect old file to new file path

Redirect https://www.phpgang.com/olddirectory/old.php https://www.phpgang.com/newdirectory/new.php

This redirect can move old page to new page.

Listing enable and disable


# allows to display directory listing
Options +Indexes
# disable directory listing
Options -Indexes
IndexIgnore *

Error Page

errorDocument 400 http://demo.phpgang.com/404.html
errorDocument 401 http://demo.phpgang.com/404.html
errorDocument 404 http://demo.phpgang.com/404.html
errorDocument 500 http://demo.phpgang.com/404.html

This code will redirect when an 400, 401, 404 or 500 error occurred it opens 404.html file.

Cache static files

<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=604800, public"

Very useful feature in .htaccess is caching of static files above example cache all images for 1 week this will increase your website’s speed.

Redirect to www




If you type your website name in browser without www it automatically add www before your domain name.

RewriteEngine On
RewriteCond %(HTTP_HOST) ! ^www.phpgang.com$ [NC] #NC - represents not case sensitive.
RewriteRule ^(.*)$ https://www.phpgang.com/$1 [L, R=301]

^ – starting
.* – one or more characters.
$ – ending
L – if rule matches, don’t process any other rewrite rules below this one.
R=302 – redirect code

There is many more about htaccess we will discuss in future.

I hope you like this tutorial if you have any suggestions and improvement please feel free to comment bellow.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.

Tutorial Categories:

22 responses to “How to Rewrite urls with .htaccess”

  1. Shahbaz Ahmed Bhatti says:

    Dear Php Gang Team

    So Many Thanks for your Post about .htaccess
    Now i got .htaccess Stand For

    Hypertext access is shortly called as htaccess,

    i have some confusion Can u Please Tell me what is $1

    Can u Please Tell me Which Code i have to Use Like this URL Example you Mention in top


    Can PhpGang upload a Source Code (.htaccess+ .php file like profile.php?user=facebook)
    Please guide me. i m trying to read this Article and understanding but having some confusin

    Once again PhpGang Love you all Team do good work

    • huzoorbux says:

      $1 is the parameter user value which you will receive in your profile.php file This is the php file.

      • Shahbaz Ahmed Bhatti says:

        Bux Sir. Can u Please tell me whihc code i should use. i m trying this one but not getting result

        RewriteEngine on

        RewriteCond %{REQUEST_FILENAME} !-d # rewrite condition if it is not directory

        RewriteCond %{REQUEST_FILENAME}.php -f # rewrite condition if it file with php extension

        RewriteRule ^(.*)$ $1.php

      • Sobin J M says:

        RewriteEngine on
        RewriteRule ^/?test/([0-9]+)$ /test.php?id=$1
        This is my .htaccess Code
        every thing working file but I cannot access the parameter value using $_GET[‘id’], it;s showing the error (Array ( [type] => 8 [message] => Undefined index: id [file] => /home/…./test.php [line] => 8 ))
        Please Help me sir.

  2. Shahbaz Ahmed Bhatti says:

    Dear Php Gang Team

    i m Trying to learn .htaccess since morning. i m getting this error on my localhost

    Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error.

    More information about this error may be available in the server error log.


    Please help me

    • huzoorbux says:

      Might be this module, is inactive on your server add following line in httpd.conf

      LoadModule rewrite_module modules/mod_rewrite.so

      and un-comment this line in httpd.conf

      RewriteEngine on

      • Vaibhav says:

        i am also suffer with this error, when i used this code on my localhost.

        • huzoorbux says:

          I suggest to use this on hosted sites not on localhost.

          Here is the solution if you are on windows

          in your httpd.conf Change this line

          #LoadModule rewrite_module modules/mod_rewrite.so
          LoadModule rewrite_module modules/mod_rewrite.so

          and enable .htaccess by changing
          AllowOverride None
          AllowOverride All

          I hope it works.

          • Vaibhav says:

            i used this code local & live but the issue is remain same 🙁

          • huzoorbux says:

            Share your htaccess code please

          • Vaibhav says:

            RewriteEngine on

            RewriteCond %{REQUEST_FILENAME} !-d # rewrite condition if it is not directory

            RewriteCond %{REQUEST_FILENAME}.php -f # rewrite condition if it file with php extension

            RewriteRule ^(.*)$ $1.php

  3. Robins says:

    Where can i find httpd.conf file?

    • huzoorbux says:

      If you are using wamp server your httpd.conf should be on this path: C:/wamp/bin/apache/apache2.2.22/conf/httpd.conf

      If you are using wamp server your httpd.conf should be on this path: C:/xampp/apache/conf/httpd.conf

  4. Shahbaz Ahmed Bhatti says:

    Dear Php Gang Team,

    Please it a request, Can u Please upload a demo and download file, so may we can test on live hosting.
    Already sent request to PhpGang Team, But not got reply yet.
    Please Sir

  5. Vaibhav says:

    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1

    i am using this code… but my url is not change… please help me.

    • huzoorbux says:

      There is a mistake in you code please you this code

      RewriteEngine on

      RewriteRule ^([a-zA-Z0-9_-]+)$ test.php?q=$1

      RewriteRule ^([a-zA-Z0-9_-]+)/$ test.php?q=$1

  6. huzoorbux says:

    Why adding =”” in start tag

    actual start tag is 
  7. sahil kumar says:

    hi i am writing this code , This is not working, Only first line code is working, Second line code is not working, Kindly help

    Options +FollowSymLinks

    RewriteEngine On

    RewriteRule ^([^/]*).php$ /company/?tag=$1

    RewriteRule ^([^/]*).php$ /product/?tag=$1

  8. om prakash says:

    Sir, rewrite rule is different for dot com or other dots(like .in,.org)

Leave a Reply

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