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:

Example Pretty URLs 

http://www.phpgang.com/profile.php?user=facebook

to

http://www.phpgang.com/facebook

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

Remove file extension .php or .html

http://www.phpgang.com/home.php

to

http://www.phpgang.com/home

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

Redirect old file to new file path

This redirect can move old page to new page.

Listing enable and disable

directory-listing-htaccess

Error Page

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

Cache static files

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

phpgang.com

to

www.phpgang.com

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

^ – 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:
  • Shahbaz Ahmed Bhatti

    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

    http://www.phpgang.com/profile.php?user=facebook
    to
    http://www.phpgang.com/facebook

    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

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

      • Shahbaz Ahmed Bhatti

        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

  • Shahbaz Ahmed Bhatti

    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, webmaster@localhost 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

      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

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

        • huzoorbux

          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
          to
          LoadModule rewrite_module modules/mod_rewrite.so

          and enable .htaccess by changing
          AllowOverride None
          to
          AllowOverride All

          I hope it works.

          • Vaibhav

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

          • huzoorbux

            Share your htaccess code please

          • Vaibhav

            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

  • Robins

    Where can i find httpd.conf file?

    • huzoorbux

      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

  • Shahbaz Ahmed Bhatti

    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

  • Vaibhav

    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

      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

  • huzoorbux

    Why adding =”” in start tag

    • Vaibhav

      sir please can you upload whole file n demo file.

  • sahil kumar

    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

  • om prakash

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

    • huzoorbux

      Same for every domain extension.

  • Awesome 🙂