Htaccess File Tutorial and Tips.
Wall Script
Follow Me:
Tuesday, November 19, 2013

Htaccess File Tutorial and Tips.

After posting Understanding of Regular Expression article most of my readers are expecting .htaccess basics, and I did not find any useful article on Google first page results. Specially I love to write .htaccess file, using this you can easily configure and redirect Apache Web Server file system. This post will explain you how to create friendly URLs, sub domain directory re-directions and many more.

Understanding Htaccess Redirect


Download Script     Live Demo

Note: .htaccess file will be in hidden format, please change your folder and file settings to view this file.

How to Create a .htaccess File?
Open any text editor application and file save as with .htaccess name and enable mod_rewrite extension in php.ini file in Apache Web Server configurations.

Default directory Listing
Enable directory Listing using htaccess

Disable directory Listing
If you want to disable folder files listing, include following code.
# Disable Directory Browsing
Options All -Indexes
Disable directory Listing using htaccess

Error Pages
Here error page is redirecting to error.html.
errorDocument 400 http://www.youwebsite.com/error.html
errorDocument 401 http://www.youwebsite.com/error.html
errorDocument 404 http://www.youwebsite.com/error.html
errorDocument 500 http://www.youwebsite.com/error.html

htaccess 404 error disply

RewriteEngine On it is turn on Rewrite Rules in Apache Server. if you want to turn off, just change the value to off.
RewriteEngine on

Domain Redirection
.htacces code for redirecting yourwebsite.com to www.yourwebsite.com
RewriteCond %{HTTP_HOST} ^yourwebsite.com
RewriteRule (.*) http://www.yourwebsite.com/$1 [R=301,L]

Sub Domain Redirection
Sub domain redirection mapping to folder. Here http://www.yourwebsite.com is connecting to website_folder folder.
RewriteCond %{HTTP_HOST} ^www\.yourwebsite\.com$
RewriteCond %{REQUEST_URI} !^/website_folder/
RewriteRule (.*) /website_folder/$1

Here http://subdomain.yourwebsite.com is connecting to subdomain_folder folder.
RewriteCond %{HTTP_HOST} ^subdomain\.yourwebsite\.com$
RewriteCond %{REQUEST_URI} !^/subdomain_folder/
RewriteRule (.*) /subdomain_folder/$1

Htaccess File Inside The Folder.

Old Domain Redirection
htaccess code for redirecting old domain(abc.com) to new domain(xyz.com). Live demo fglogin.com is now redirecting to oauthlogin.com
RewriteCond %{HTTP_HOST} ^abc.com
RewriteRule (.*) http://www.xyz.com/$1 [R=301,L]

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

Friendly URLs
Friendly/Pretty URLs help in search engine rankings.

Profile URL
Profile parameter allows [a-zA-Z0-9_-] these inputs. More help read Understanding Regular Expression
http://labs.9lessons.info/profile.php?username=srinivas
to
http://labs.9lessons.info/srinivas

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

Messages URL
http://labs.9lessons.info/messages.php?message_username=srinivas
to
http://labs.9lessons.info/messages/srinivas

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

Friends URL
http://labs.9lessons.info/friends.php?username=srinivas
to
http://labs.9lessons.info/friends/srinivas
RewriteRule ^friends/([a-zA-Z0-9_-]+)$ friends.php?username=$1
RewriteRule ^friends/([a-zA-Z0-9_-]+)/$ friends.php?username=$1

Friends URL with Two Parameters
Here the first parameter allows [a-zA-Z0-9_-] and second parameter allows only number [0-9]
http://labs.9lessons.info/friends.php?username=srinivas&page=2
to
http://labs.9lessons.info/friends/srinivas/2

RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)$ friends.php?username=$1&page=$2
RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)/$ friends.php?username=$1&page=$2

Hiding File Extension
http://www.yourwebsite.com/index.html
to
http://www.yourwebsite.com/index
RewriteRule ^([^/.]+)/?$ $1.html

80 comments:

  1. thanks..how to redirect site to yoursite.com to www.yoursite.com
    in wordpress can do..how to do with htaccess

    ReplyDelete
  2. The last part is pretty unnecessary.. creating a new redirect for every single action?
    In my opinion the best way is to redirect EVERYTHING to index.php, and let PHP parse the action
    RewriteRule ^(.*)$ index.php/$1 [L]
    So domain.com/some/thing/3?id=15 becomes domain.com/index.php/some/thing/3?id=15
    Doing so you can fetch the data from the request URI

    ReplyDelete
  3. Nice break down, you know what you could also add, restrictions by denying access to specific folder, files or entire root using allow/deny directives. Or using password protected folders too.

    Have on my site in the top first menu some .htaccess tools myself if you want to take some examples and complete the article :)

    Cheers.

    ReplyDelete
  4. great job as always !

    ReplyDelete
  5. Thank you so much! I really needed this.

    ReplyDelete
  6. I have been looking for exactly this for ages! thanks

    ReplyDelete
  7. Believe me Srini I spent almost 3 days trying to fix 404 Not found in wordpress. I was worrying about this and your newsletter popped up on my phone '9 Lessons: Htaccess file tutorial and tips.' I just logged into your website and got my fix :) I like you for the sake of knowledge sharing :)

    ReplyDelete
  8. how use htaccess to prevent Internet download manager from capture .mp4 file extension?

    ReplyDelete
  9. Instead of two rules to math the leading slash you could add it in one rule like this: RewriteRule ^([a-zA-Z0-9_-]+)/?$ profile.php?username=$1

    ReplyDelete
  10. Hi bro,

    how can we add trailing slash to like this

    http://www.yourwebsite.com/index.html to http://www.yourwebsite.com/index/



    ReplyDelete
  11. Awesome explanation, this post will help many more like me. Thankyou

    ReplyDelete
  12. i want to change space with hyphen means...
    http://www.yourwebsite.com/sub directory to
    http://www.yourwebsite.com/sub-directory

    ReplyDelete
  13. Thank you very much !
    Best .htaccess tutorial

    ReplyDelete
  14. Hi,
    can you explain us, how to configure an .htaccess with a desktop version and a mobile version please ?

    Thanks :)

    ReplyDelete
  15. Srini,

    Say, how do we restrict website download like HTTrack

    ReplyDelete
  16. Thanks for the post,
    I agree, its difficult to find .htaccess simple tutorial.
    Your tutorial will be very helpful.

    ReplyDelete
  17. Thanks a lot for this important tutorial. I tried many tips of it and they work very well, but when I use friendly urls I get the following error. Any help?

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

    ReplyDelete
  18. Very Nice Article,

    How to Redirect Particular Url to Http from Https

    "https://www.srinu/pages/account.php" To
    "http://www.srinu/pages/account.php".


    ReplyDelete
  19. HTTP to HTTPS redireciton


    if (top.location!= self.location) {
    top.location = 'https://www.yourwebsite.com'
    }

    ReplyDelete
  20. Awesome i really appreciate your hard work , the wall script is too awesome

    ReplyDelete
  21. Thanks Sri

    Helpful tutorial

    ReplyDelete
  22. Nice Article Shinivas. After reading this I came to know so many things which I didn't know.
    Thanks for sharing with us. :)

    ReplyDelete
  23. not working any single rule don't know whats going wrong no internal server error nothing but no effect takes place.

    ReplyDelete
  24. Needs an admin page so u can monitor everything. I also made a gallery for each person so it has a private and shared gallery and i found some server hosts dont allow clean urls. Would like to see a cover photo. guess i could do it myself but dont have time really. But over all very good love to see where it goes next awesome work.

    ReplyDelete
  25. hi srinivasan...i wanted multiple checkbox functionality with radio button,price ranger and currency calculation for ecommerce website can you help me out

    ReplyDelete
  26. Thnaks bro ur post has helped me to solve .htaccess issue :)

    ReplyDelete
  27. Very useful and knowledgeable information,your articles are quite informative and helpful.Great work

    ReplyDelete
  28. hi

    I appreciate your post and good job, please how can i create a page for users, e.g. www.mywesite.com/romeo (the name there is the user of my website) Facebook does this for it users.

    Please kindly assist.

    ReplyDelete
  29. thank for sharing this tutorial broo,,,,
    i can start now

    ReplyDelete
  30. many times I mess up with .htaccess file & creates many errors for my blog. Your post is very useful..thanks a lot.

    ReplyDelete
  31. Thanks for you supporting .....

    ReplyDelete
  32. Nice Article
    How to Redirect HTTPS to HTTP particular page.

    "https://www.myname/pages/account.php" To
    "http://www.myname/pages/account.php".

    ReplyDelete
  33. How i can pass third variable in htaccess

    ReplyDelete
  34. can you explain little more detail it won't work on my site
    home.php?view=x
    to change home/x

    ReplyDelete
  35. thanku , it awesome

    ReplyDelete
  36. It is exactly the thing that I was looking for. Thanks a lot for this article :)

    ReplyDelete

  37. RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)$ friends.php?username=$1&page=$2
    RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)/$ friends.php?username=$1&page=$2

    compare with:
    RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)/?$ friends.php?username=$1&page=$2

    ReplyDelete
  38. enable mod_rewrite extension in php.ini file? You meant enable Apache module mod_rewrite.

    ReplyDelete
  39. This comment has been removed by the author.

    ReplyDelete
  40. thanks man u r great

    ReplyDelete
  41. Hi Bro,
    Is it possible to rewrite variable name as subdomain
    For Example:
    http://9lessons.info/profile.php?username=srinivas
    Rewrite as
    http://srinivas.9lessons.info/

    ReplyDelete
  42. cool..!!great work dude..!! :) thanks ..!!

    ReplyDelete
  43. RewriteEngine On
    RewriteRule ^gallery/([a-zA-Z0-9_-]+)$ img_gallery.php?fid=$1
    RewriteRule ^gallery/([a-zA-Z0-9_-]+)/$ img_gallery.php?fid=$1

    i try it but no use can you help me my site is

    http://gallery.teluguwow.com/index.php

    ReplyDelete
  44. my html & .htaccess file data folder root directory

    plz help me to

    my address www.----.com/data/p1.html i want it ru as www.----.com/data/p1

    my .htaccess file code is

    RewriteEngine on
    RewriteRule ^([^/.]+)/?$ $1.html


    plz help me..

    ReplyDelete
  45. how to add special characters in
    I am using cat & subcat values in base64_encode() form like OQ==
    RewriteRule ^product/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ product.php?cat=$1&subcat=$2
    RewriteRule ^product/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ product.php?cat=$1&subcat=$2

    ReplyDelete
  46. Thank you very much Srinivas..
    This is one the best tutorial for .htaccess file..

    ReplyDelete
  47. How about change this url:

    http://hajju.admjkt.sch.id/admin/?page=topik

    to

    http://hajju.admjkt.sch.id/admin/topik

    Please help me!!!

    ReplyDelete