Htaccess File Tutorial and Tips.
Wall Script
Wall Script
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 https://www.youwebsite.com/error.html
errorDocument 401 https://www.youwebsite.com/error.html
errorDocument 404 https://www.youwebsite.com/error.html
errorDocument 500 https://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 (.*) https://www.yourwebsite.com/$1 [R=301,L]

Sub Domain Redirection
Sub domain redirection mapping to folder. Here https://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 https://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 (.*) https://www.xyz.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.abc\.com
RewriteRule (.*) https://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
https://labs.9lessons.info/profile.php?username=srinivas
to
https://labs.9lessons.info/srinivas

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

Messages URL
https://labs.9lessons.info/messages.php?message_username=srinivas
to
https://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
https://labs.9lessons.info/friends.php?username=srinivas
to
https://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]
https://labs.9lessons.info/friends.php?username=srinivas&page=2
to
https://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
https://www.yourwebsite.com/index.html
to
https://www.yourwebsite.com/index
RewriteRule ^([^/.]+)/?$ $1.html
web notification

91 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. Very nice , Thanks.

    ReplyDelete
  18. 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
  19. 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
  20. HTTP to HTTPS redireciton


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

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

    ReplyDelete
  22. Thanks Sri

    Helpful tutorial

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

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

    ReplyDelete
  25. 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
  26. hi srinivasan...i wanted multiple checkbox functionality with radio button,price ranger and currency calculation for ecommerce website can you help me out

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

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

    ReplyDelete
  29. 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
  30. thank for sharing this tutorial broo,,,,
    i can start now

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

    ReplyDelete
  32. Thanks for you supporting .....

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

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

    ReplyDelete
  34. How i can pass third variable in htaccess

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

    ReplyDelete
  36. thanku , it awesome

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

    ReplyDelete

  38. 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
  39. enable mod_rewrite extension in php.ini file? You meant enable Apache module mod_rewrite.

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

    ReplyDelete
  41. thanks man u r great

    ReplyDelete
  42. 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
  43. cool..!!great work dude..!! :) thanks ..!!

    ReplyDelete
  44. 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
  45. 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
  46. 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
  47. Thank you very much Srinivas..
    This is one the best tutorial for .htaccess file..

    ReplyDelete
  48. 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
  49. It is exactly the thing that I was looking for. Thanks a lot for this article :)

    ReplyDelete
  50. sir, i want to remove file extension like .php and query string together. please tell me how can i do.

    ReplyDelete
  51. how to use 3 parameter in htaccess please give suggestion

    ReplyDelete
  52. Does anyone know the equivalent of htaccess on IIS??? I nedd to do something like RewriteRule.

    ReplyDelete
  53. Thanks A Lot its very useful for me

    ReplyDelete
  54. Its really very useful post for beginner as well as experienced programmers.
    Thanks.

    ReplyDelete
  55. How do i set rule in.htaccess if my url like this
    example.com/aboutus using wordpress permalinks ?

    ReplyDelete
  56. how to GET value parameter in clean url if url http://labs.9lessons.info/friends/srinivas ?

    ReplyDelete
  57. sir in my server the file that on root directory .htaccess is creating problem. my site is not open if i remove this file then site is running can you help me. why this happing

    ReplyDelete
  58. Hello Sir , please how can i set it to be creating a subdomain for each user if am using php as my server side language.

    ReplyDelete
    Replies
    1. https://www.9lessons.info/2016/03/how-to-create-dynamic-subdomains-using-php-and-htaccess.html

      Delete

mailxengine Youtueb channel
Make in India
X