9lessons programming blog
Loading Search
9lessons Bring Friends
Wall Script
Follow Me:
Wednesday, November 20, 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
Comments
{ 70 comments }
ravi ranjan said...

Great

araf said...

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

Anonymous said...

Thanks a lot..

Anonymous said...

Very helpful

Anonymous said...

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

Bogdan said...

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.

Anonymous said...

great job as always !

harry suherman said...

Nice...tanks bro

Anonymous said...

Thank you so much! I really needed this.

alan said...

I have been looking for exactly this for ages! thanks

King Lucas said...

Gzip? Compression? Cache?

Ashik Basheer said...

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 :)

มานพ กองอุ่น said...

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

brodos said...

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

suresh kumar said...

Hi bro,

how can we add trailing slash to like this

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



wbdd said...

thanks :)

varatha raj said...

thanks

Rajendra Yadav said...

Great

krishna said...

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

keshav said...

Very helpful

Nilay Mehta said...

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

Anil Kumar Panigrahi said...

Nice explaination ...

greekcoder said...

Thank you very much !
Best .htaccess tutorial

Charles Notarangelo said...

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

Thanks :)

Anonymous said...

Srini,

Say, how do we restrict website download like HTTrack

Dean said...

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

Sagar said...

good yaar

Savita Kumthe said...

nice thank you

Savita Kumthe said...

great thank you

Pawan Kumar said...

Very nice , Thanks.

Anonymous said...

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.

Anonymous said...

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".


Srinivas Tamada said...

HTTP to HTTPS redireciton


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

Pavan Deshpande said...

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

Ravinder said...

Thanks Sri

Helpful tutorial

S u n i L said...

Awesome

tushar said...

nyc article

Anup said...

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

Anonymous said...

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

Emre Rothzerg said...

Good article!

Anonymous said...

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.

Anonymous said...

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

aashish said...

Good

ajit said...

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

Jasriadi Yadi said...

good

TOHID said...

beautiful........

TOHID said...

Beautiful......

shamsher khan said...

very useful post... thanks a lot

Bergin Raj said...

good

lucky Jodha said...

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

ck-romeo ofem said...

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.

Aan said...

thank for sharing this tutorial broo,,,,
i can start now

TechGopal said...

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

Anonymous said...

Thanks for you supporting .....

Anonymous said...

Nice Article
How to Redirect HTTPS to HTTP particular page.

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

Gurwinder singh said...

thanks

jaydeep namera said...

How i can pass third variable in htaccess

Gowtham Mailvaganam said...

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

Anonymous said...

thanku , it awesome

Divy Singh Rathore said...

Wow its very helpfull

Ritesh Saini said...

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

Anonymous said...


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

Anonymous said...

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

Rakan Awajan said...
This comment has been removed by the author.
raza said...

thanks man u r great

vasanth kumar said...

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/

Guru said...

Thanks

Anonymous said...

Thanks :)

jagadeesh said...

thank you very much..........

Anonymous said...

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

Post a Comment