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.
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
Disable directory Listing
If you want to disable folder files listing, include following code.
# Disable Directory Browsing
Options All -Indexes
Options All -Indexes
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
errorDocument 401 https://www.youwebsite.com/error.html
errorDocument 404 https://www.youwebsite.com/error.html
errorDocument 500 https://www.youwebsite.com/error.html
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]
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
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
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]
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
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
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
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
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
thanks..how to redirect site to yoursite.com to www.yoursite.com
ReplyDeletein wordpress can do..how to do with htaccess
Thanks a lot..
ReplyDeleteVery helpful
ReplyDeleteThe last part is pretty unnecessary.. creating a new redirect for every single action?
ReplyDeleteIn 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
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.
ReplyDeleteHave on my site in the top first menu some .htaccess tools myself if you want to take some examples and complete the article :)
Cheers.
great job as always !
ReplyDeleteNice...tanks bro
ReplyDeleteThank you so much! I really needed this.
ReplyDeleteI have been looking for exactly this for ages! thanks
ReplyDeleteGzip? Compression? Cache?
ReplyDeleteBelieve 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 :)
ReplyDeletehow use htaccess to prevent Internet download manager from capture .mp4 file extension?
ReplyDeleteInstead 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
ReplyDeleteHi bro,
ReplyDeletehow can we add trailing slash to like this
http://www.yourwebsite.com/index.html to http://www.yourwebsite.com/index/
thanks :)
ReplyDeletethanks
ReplyDeleteGreat
ReplyDeleteAwesome explanation, this post will help many more like me. Thankyou
ReplyDeleteVery helpful
ReplyDeletei want to change space with hyphen means...
ReplyDeletehttp://www.yourwebsite.com/sub directory to
http://www.yourwebsite.com/sub-directory
Nice explaination ...
ReplyDeleteThank you very much !
ReplyDeleteBest .htaccess tutorial
Hi,
ReplyDeletecan you explain us, how to configure an .htaccess with a desktop version and a mobile version please ?
Thanks :)
Srini,
ReplyDeleteSay, how do we restrict website download like HTTrack
Thanks for the post,
ReplyDeleteI agree, its difficult to find .htaccess simple tutorial.
Your tutorial will be very helpful.
good yaar
ReplyDeletenice thank you
ReplyDeletegreat thank you
ReplyDeleteVery nice , Thanks.
ReplyDeleteThanks 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?
ReplyDeleteInternal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Very Nice Article,
ReplyDeleteHow to Redirect Particular Url to Http from Https
"https://www.srinu/pages/account.php" To
"http://www.srinu/pages/account.php".
HTTP to HTTPS redireciton
ReplyDeleteif (top.location!= self.location) {
top.location = 'https://www.yourwebsite.com'
}
Awesome i really appreciate your hard work , the wall script is too awesome
ReplyDeleteThanks Sri
ReplyDeleteHelpful tutorial
Awesome
ReplyDeletenyc article
ReplyDeleteNice Article Shinivas. After reading this I came to know so many things which I didn't know.
ReplyDeleteThanks for sharing with us. :)
not working any single rule don't know whats going wrong no internal server error nothing but no effect takes place.
ReplyDeleteGood article!
ReplyDeleteNeeds 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.
ReplyDeletehi srinivasan...i wanted multiple checkbox functionality with radio button,price ranger and currency calculation for ecommerce website can you help me out
ReplyDeleteGood
ReplyDeleteThnaks bro ur post has helped me to solve .htaccess issue :)
ReplyDeletegood
ReplyDeletebeautiful........
ReplyDeleteBeautiful......
ReplyDeletevery useful post... thanks a lot
ReplyDeletegood
ReplyDeleteVery useful and knowledgeable information,your articles are quite informative and helpful.Great work
ReplyDeletehi
ReplyDeleteI 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.
thank for sharing this tutorial broo,,,,
ReplyDeletei can start now
many times I mess up with .htaccess file & creates many errors for my blog. Your post is very useful..thanks a lot.
ReplyDeleteThanks for you supporting .....
ReplyDeleteNice Article
ReplyDeleteHow to Redirect HTTPS to HTTP particular page.
"https://www.myname/pages/account.php" To
"http://www.myname/pages/account.php".
thanks
ReplyDeleteHow i can pass third variable in htaccess
ReplyDeletecan you explain little more detail it won't work on my site
ReplyDeletehome.php?view=x
to change home/x
thanku , it awesome
ReplyDeleteWow its very helpfull
ReplyDeleteIt is exactly the thing that I was looking for. Thanks a lot for this article :)
ReplyDelete
ReplyDeleteRewriteRule ^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
enable mod_rewrite extension in php.ini file? You meant enable Apache module mod_rewrite.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletethanks man u r great
ReplyDeleteHi Bro,
ReplyDeleteIs it possible to rewrite variable name as subdomain
For Example:
http://9lessons.info/profile.php?username=srinivas
Rewrite as
http://srinivas.9lessons.info/
Thanks
ReplyDeleteThanks :)
ReplyDeletethank you very much..........
ReplyDeletecool..!!great work dude..!! :) thanks ..!!
ReplyDeletethanks very much...:)
ReplyDeletenice... thanks
ReplyDeleteRewriteEngine On
ReplyDeleteRewriteRule ^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
my html & .htaccess file data folder root directory
ReplyDeleteplz 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..
thaaaaaaaaaaaaaaaaaaanks
ReplyDeletehow to add special characters in
ReplyDeleteI 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
Works perfect !
ReplyDeleteThank you very much Srinivas..
ReplyDeleteThis is one the best tutorial for .htaccess file..
How about change this url:
ReplyDeletehttp://hajju.admjkt.sch.id/admin/?page=topik
to
http://hajju.admjkt.sch.id/admin/topik
Please help me!!!
It is exactly the thing that I was looking for. Thanks a lot for this article :)
ReplyDeletesir, i want to remove file extension like .php and query string together. please tell me how can i do.
ReplyDeletehow to use 3 parameter in htaccess please give suggestion
ReplyDeleteDoes anyone know the equivalent of htaccess on IIS??? I nedd to do something like RewriteRule.
ReplyDeleteThanks A Lot its very useful for me
ReplyDeleteNice Post.. Thanks
ReplyDeleteIts really very useful post for beginner as well as experienced programmers.
ReplyDeleteThanks.
How do i set rule in.htaccess if my url like this
ReplyDeleteexample.com/aboutus using wordpress permalinks ?
thanx
ReplyDeletehow to GET value parameter in clean url if url http://labs.9lessons.info/friends/srinivas ?
ReplyDeletesir 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
ReplyDeleteHello Sir , please how can i set it to be creating a subdomain for each user if am using php as my server side language.
ReplyDeletehttps://www.9lessons.info/2016/03/how-to-create-dynamic-subdomains-using-php-and-htaccess.html
Delete