4

I am trying to send every request to www.example.com/user/ to www.example.com/user.php?id=0 using this

RewriteRule ^user/$ user.php?id=0

Basically, if someone is accessing www.example.com/user/ with no user id, the site will default to id = 0.

However, when I type www.example.com/user/ Apache seems to simply serve the user.php file, completely ignoring the RewriteRule. Any idea on why this is happening?

Thank you.

I should mention that this only happens if I use the same word in the URL as the php file's name. For example, if I were to use

RewriteRule ^yes/$ user.php?id=0

Going to www.example.com/yes/ would apply the RewriteRule just fine. So it seems that Apache looks for a file with that name and ignores the RewriteRule. And no, adding a [L] flag did not help.

Here's my .htaccess:

RewriteEngine On
RewriteRule ^user/$ user.php?id=0
RewriteRule ^user/([0-9]+)$ user.php?id=$1 
5
  • where you are putting the user id if provided? like this : www.example.com/user/12345 Nov 26, 2011 at 15:14
  • I do have a separate rule for that: RewriteRule ^user/([0-9]*)$ user.php?id=$1
    – user1066994
    Nov 26, 2011 at 15:15
  • do you have a folder call user? Nov 26, 2011 at 15:23
  • sure RewriteEngine On RewriteRule ^user/$ user.php?id=0 RewriteRule ^user/([0-9]+)$ user.php?id=$1
    – user1066994
    Nov 26, 2011 at 15:32
  • GREAT NEWS! I managed to test the code on a remote server and it works. So all the bits of code are OK. It seems that my laptop's LAMP install is to blame :| DAMN IT!!!
    – user1066994
    Nov 26, 2011 at 16:22

2 Answers 2

6

try this:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^user/$ user.php?id=0 [L,NC,QSA]
RewriteRule ^user/([0-9]+)/?$ user.php?id=$1 [L,NC,QSA]

The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl, or the break command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules.

from: http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l

7
  • Tried the [L] flag, but Apache still seems to serve the file without any $_GET parameters. Basically, if I go to www.example.com/user/ it just says Notice: Undefined index: id in /var/www/image-jam/user.php on line 4
    – user1066994
    Nov 26, 2011 at 15:30
  • please post your .htaccess. you have a conflict with another rule that does not probably have the [L] flag Nov 26, 2011 at 15:32
  • you don't have any other rules for example in your httpd.conf? what's at line 4? Nov 26, 2011 at 15:37
  • Nope, no other rules. Line 4 is $id = $_GET["id"];
    – user1066994
    Nov 26, 2011 at 15:39
  • if you try something like: RewriteRule ^test/?$ user.php?id=0 [L] and you use: www.example.com/test/1 does it work? I updated my answer maybe the rewritebase will work Nov 26, 2011 at 15:43
0

I think your rewrite rules are in the wrong order, and you're not using the [L] flag to tell apache not to run any more rules when a rule's been matched. Also you could use the + operator instead of * to match at least one digit in your second rule:

RewriteRule ^user/$ user.php?id=0 [L]
RewriteRule ^user/([0-9]+)$ user.php?id=$1 [L]
3
  • Sorry, [L] flag did not do the trick. Apache seems to simply serve the file.
    – user1066994
    Nov 26, 2011 at 15:26
  • 1
    I can only guess that there's another rule on your .htaccess interfering...any chance you can post all the rules you have in your original question?
    – Clive
    Nov 26, 2011 at 15:28
  • Don't really think there's a conflict. But I did post the .htaccess file.
    – user1066994
    Nov 26, 2011 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.