Take the 2-minute tour ×


I'm trying to install a local version of my website onto my laptop so I can do development work without terrifying my users and exploding the live site.

I went ahead and installed ubuntu on my laptop along with apache/mysql/php and downloaded my php files and database from my web server onto my laptop.

I went ahead and changed my etc/hosts file so my website domain name now points to my localhost and that seems to be working great.

However, for the life of me, I can't get my htaccess mod_rewrite directives to work. It is very frustrating because I know mod_rewrite is working since I went though a tutorial on turning it on in ubuntu (the tutorial included a simple test case to make sure it was running properly). The htaccess code works perfectly on my actual web server, but it just won't work on my local ubuntu install.

But enough talk, I'll let you see what I have in the htaccess file to whet your appetite:

# suphp_configpath /home/yourusername/path/to/php.ini

AuthUserFile /var/www/.htpasswd
AuthType Basic
AuthName "not allowed"

<Files ".htaccess">
  Require valid-user

<Files "uploadpage.php">
  Require valid-user

<Files "upload.php">
  Require valid-user

ErrorDocument 404 /404.php

Options +FollowSymlinks 
RewriteEngine On
RewriteBase /
RewriteRule ^random/([0-9]+)/(.*)$ random.php?id=$1
RewriteRule ^new/([0-9]+)/(.*)$ new.php?id=$1
RewriteRule ^quickviewrandom/([0-9]+)/(.*)$ quickviewrandom.php?id=$1
RewriteRule ^quickviewnew/([0-9]+)/(.*)$ quickviewnew.php?id=$1
RewriteRule ^archive/previous-30-days/$ archive/previous-30-days.php
RewriteRule ^archive/previous-12-months/$ archive/previous-12-months.php
RewriteRule ^user/profile/(.*)/$ user/profile.php?username=$1
RewriteRule ^post/([0-9]+)/(.*)$ post.php?id=$1&%{QUERY_STRING}
RewriteRule ^direct/([0-9]+)/(.*)$ direct.php?id=$1
RewriteRule ^comments/([0-9]+)/(.*)$ comments.php?id=$1&%{QUERY_STRING}
RewriteRule ^top/([0-9]+)/$ top.php?page=$1
RewriteRule ^topweek/([0-9]+)/$ topweek.php?page=$1
RewriteRule ^fresh/([0-9]+)/$ fresh.php?page=$1
RewriteRule ^user/submitform/$ user/submitform.php
RewriteRule ^user/submit/(.*)/([0-9]+)/$ user/submit.php?username=$1&postlimit=$2
RewriteRule ^user/usercomments/(.*)/([0-9]+)/$ user/usercomments.php?username=$1&commentlimit=$2
RewriteRule ^user/favorites/(.*)/([0-9]+)/$ user/favorites.php?username=$1&favlimit=$2
RewriteRule ^user/friends/(.*)/([0-9]+)/$ user/friends.php?username=$1&friendlimit=$2
RewriteRule ^user/inbox/([0-9]+)/$ user/inbox.php?maillimit=$1&%{QUERY_STRING}
RewriteRule ^user/message/([0-9]+)/$ user/message.php?stackid=$1
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]

RewriteCond %{QUERY_STRING} img_name=(.*) 
RewriteRule ^new\.php$ http://www.mydomain.com/404.php [R=301]
RewriteRule ^page\.php$ http://www.mydomain.com/404.php [R=301,L]

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

Any thoughts on what I should try? Seriously, I'm open to anything at this point.

Strangely enough this rewrite rule works:


This was part of the tutorial I did for setting up mod_rewrite in ubuntu. The rest of the rules are my rules that are not working :-(

share|improve this question

migrated from stackoverflow.com Oct 12 '11 at 2:22

This question came from our site for professional and enthusiast programmers.

I removed the PHP tag as this question has no reference to PHP. –  vascowhite Oct 11 '11 at 0:04
For future reference, the mod_rewrite log is great for debugging this sort of thing. Read up on the RewriteLog directive and its friends. –  David Z Oct 11 '11 at 12:08
Please post the link from your tutorial, because I'm pretty sure there's something missing is this tutorial, or you just missed something. –  Olivier Pons Oct 13 '11 at 5:29

1 Answer 1

Running a separate development server space, along with other items such as proper use of version control are a sign of a serious programmer, so you are to be commended and reminded that the pain you spend getting this right is better than the pain of mistakes made while developing on a live site.

In this case obviously there is an assumption being made somewhere which does not correspond to reality, some difference between the live and development environments. Keeping that in mind, I would build up from the example that does work and slowly add in conditions, tweaking until the next most complex thing works, looking for the clue as to what may be amiss.

Try some simple rules with the RewriteCond. Try without the RewriteBase directive. Try with and without the leading anchor "^".

share|improve this answer
That little complement you threw in there at the start meant a lot to me. I'm new at this, and it has been a LONG day. I'll take your advice and shoot you a followup (probably tomorrow morning as I'm struggling to hold my head up now). Do you have experience with ubuntu (I'm running the latest version) and htaccess? –  billmalarky Oct 11 '11 at 0:16
Also, any idea why that one rule (the tutorial rule) would be working but the original rules from my actual websites htaccess file are not? –  billmalarky Oct 11 '11 at 0:22
I can't really see enough of your system to have a firm idea, I'm afraid. I maintain an Ubuntu system and a couple of CentOS systems, but the vast bulk of my servers, development, testing and production, run OpenBSD. But I don't want to start a thread on that; mod_rewrite should work about the same on any *nix system. The complement is honest and partly for my own benefit. As developers we work on each others projects sooner or later, and the better job someone else has done, the more sane my job will be. –  Devin Ceartas Oct 11 '11 at 0:24
Well I've managed to stay awake long enough to find what I believe to be a symptom. If I remove slashes "/" from the rewrite rule, it seems to work (note this also explains why the tutorial rule worked and my more complex rules didn't). Any idea why apache is being so finicky about the slashes? –  billmalarky Oct 11 '11 at 0:36
random/([0-9]+)/(.*) should only match if there is a slash at the end. But it isn't clear that you need it. If you don't need there to be a slash at the end, then just use random/([0-9]+) -- it's usually best to use only what you actually need. –  Devin Ceartas Oct 11 '11 at 2:09

Your Answer


