At first I should define, what is mod_rewrite?
mod_rewrite is a part of Apache server that can rewrite requested urls on the fly.
To enable mod_rewrite in Ubuntu, you just need to write this command in terminal
sudo a2enmod rewrite
After enabling mod_rewrite you can write .htaccess file for your web application.
So what is .htaccess?
.htaccess file provides a way to make configuration changes on a per directory basis. It is a file contains configuration directives is placed in a particular document directory and the directives apply to that directory and all subdirectories thereof.
Some example:
Nice looking URLs (no querying) with pagination:
Suppose your url is: domain.com/article.php?name=title&page=5
You want to change: domain.com/articles/title/5/
Then write in .htaccess file:
RewriteRule ^articles/(A-Za-z0-9-]+)/([0-9]+)/?$ article.php?name=$1&page=$2 [L]
The rule is defined in regular expression. Here [L] means Last Rule. It’s called RewriteRule Flags.
Another example:
Suppose your site has permanently moved to a new domain.
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [R=301, L]
Here [NC] means case insensitive and its called RewriteCond Flags. [R=301] means moved permanently. Its called redirection header code.
February 17, 2009 at 2:07 pm
One thing you might’ve forgotten: when dealing with default installation, one should also change AllowOverRide None in /etc/apache/sites-available/default to AllowOverRide All(or something else, but not None). Apache won’t read the rewrite rules nor the .htaccess-files if AllowOverRide is set to None.
December 28, 2012 at 2:03 am
Thanks @rimmer333 . i’ve try it, but when I change AllowOverRide to All I can’t access my localhost instead & I had “permission denied” message on my browser.
any idea??
Sorry for my english & thanks 😉
May 26, 2009 at 12:04 pm
This worked perfectly for me but once I did what @rimmer333 suggested.
Cheers all,
Tom
December 2, 2009 at 10:22 pm
Thanks mahmud and rimmer333, this was the info I was looking for. Cheers!
March 12, 2010 at 2:39 pm
Yep, thank you mahmud and rimmer333 that’s what worked, donot forget the rimmer333 advice guys 🙂
August 13, 2010 at 7:41 am
Yippee!!! Thanks a lot mahmud and rimmer333!!! It worked for me so nicely… I’ve been trying this for years… 🙂
October 3, 2010 at 9:43 pm
Just what i needed – thank you!
October 22, 2010 at 4:36 pm
Thanks two both of you, worked great
February 4, 2011 at 11:38 am
Thanks dear. Always I used this user guide when I want to enable rewrite in ubuntu
February 9, 2011 at 4:02 am
Thanks to mahmud and rimmer333 that worked a treat.
February 10, 2011 at 4:14 pm
I had to his problem too. but the issue was down to user priviledges. I you are working on say an Ubuntu workstation and your user name is “johndoe” log out and log in as ‘root’. I had an issue with Drupal 7 not showing the “Clean URL” in the config as my user name, I tried the above and nothing changed When I logged out and in as ‘root’ and went to the same function, it was there and worked 1st time aok.
Worth a try if you are really scratching ones head in like ‘what the pheck is going on!” moment.
I wasted a good 2-3 hours trying to work it out.
Paul B
March 14, 2011 at 6:41 pm
Hey, thank you very much, this worked as exspected! Thank you very very much! 🙂
April 13, 2011 at 3:06 pm
Thanks man. That worked fantabolously (made this word up) great.
May 28, 2011 at 3:47 am
thanks, its very usefull
May 28, 2011 at 3:57 am
[…] kepada Think Different di artikel yang berjudul Ubuntu – Enable mod_rewrite in Apache server semoga bisa membantu yang lain juga. from → Internet ← membuat slug atau permalink […]
July 27, 2011 at 1:38 am
[…] https://mahmudahsan.wordpress.com/2008/07/06/ubuntu-enable-mod_rewrite-in-apache-server/ […]
January 27, 2012 at 7:13 am
Thanks to all, Worked perfectly.
once again thanks to mahmud ahsan and rimmer333
March 5, 2012 at 5:02 pm
I love looking through an article that will make men and women think. Also, thank you for allowing for me to comment!
June 27, 2012 at 11:58 pm
very good post….it really help us to understand the fact. Keep it up.
October 23, 2012 at 12:42 pm
http://www.internetten-para-kazanmak-seo.com/
May 10, 2013 at 10:57 pm
[…] https://mahmudahsan.wordpress.com/2008/07/06/ubuntu-enable-mod_rewrite-in-apache-server/ […]
September 13, 2013 at 6:35 pm
Thanks mahmud and rimmer333!! This saved me from pulling an Office Space scene on my server… 🙂
September 23, 2013 at 7:13 pm
Hello! I know this is kinda off topic however I’d figured I’d ask.
Would you be interested in trading links or maybe guest authoring a blog post or vice-versa?
My blog discusses a lot of the same topics as yours and I
think we could greatly benefit from each other. If you happen to be interested feel free to shoot
me an e-mail. I look forward to hearing from you!
Fantastic blog by the way!
December 26, 2013 at 4:06 pm
Great beat ! I would like to apprentice while you amend your
website, how can i subscribe for a blog website?
The account aided me a acceptable deal. I had been a little bit acquainted of
this your broadcast provided bright clear idea
January 11, 2014 at 8:44 pm
I wanted to post you that little word to finally say thanks once again relating to the unique things you’ve provided in this article.