Ubuntu – Enable mod_rewrite in Apache server

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.

25 Responses to “Ubuntu – Enable mod_rewrite in Apache server”

  1. rimmer333 Says:

    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.

    • Fery Wardiyanto Says:

      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 😉

  2. Tom Says:

    This worked perfectly for me but once I did what @rimmer333 suggested.

    Cheers all,

  3. Morgan Says:

    Thanks mahmud and rimmer333, this was the info I was looking for. Cheers!

  4. evgeny Says:

    Yep, thank you mahmud and rimmer333 that’s what worked, donot forget the rimmer333 advice guys 🙂

  5. Praveen Kumar Says:

    Yippee!!! Thanks a lot mahmud and rimmer333!!! It worked for me so nicely… I’ve been trying this for years… 🙂

  6. JR Says:

    Just what i needed – thank you!

  7. Dan Luis Says:

    Thanks two both of you, worked great

  8. Ananda Says:

    Thanks dear. Always I used this user guide when I want to enable rewrite in ubuntu

  9. Ross Says:

    Thanks to mahmud and rimmer333 that worked a treat.

  10. Paul B Says:

    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

  11. Alister Frey Says:

    Hey, thank you very much, this worked as exspected! Thank you very very much! 🙂

  12. Usman Ajmal Says:

    Thanks man. That worked fantabolously (made this word up) great.

  13. anang Says:

    thanks, its very usefull

  14. Mengaktifkan .htaccess (Enable mod_rewrite) Server Apache di Ubuntu « My DeveLoveR Blog Says:

    […] 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 […]

  15. Sri Says:

    Thanks to all, Worked perfectly.
    once again thanks to mahmud ahsan and rimmer333

  16. Facebook for business Says:

    I love looking through an article that will make men and women think. Also, thank you for allowing for me to comment!

  17. hostlift Says:

    very good post….it really help us to understand the fact. Keep it up.

  18. M Ford Says:

    Thanks mahmud and rimmer333!! This saved me from pulling an Office Space scene on my server… 🙂

  19. audioboo.fm Says:

    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!

  20. Dillon Boothe Says:

    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

  21. Hyman Propst Says:

    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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: