Halter
Halter

Reputation: 48

Issue with mod_rewrite query string rule

My issue is pretty simple however as I am new to mod_rewrite I seem to be very confused. basically I am trying to make vision friendly urls with mod_rewrite. ie: 'http://example.com/user.php?id=3' works the same as 'user/7854' etc. i currently have a regex rule as so:

RewriteEngine on 
RewriteRule ^user/([a-z]+)/?$ user.php?id=$1  

phpinfo() confirms that mod_rewrite is in fact operating/working. My understanding of this is that, if the user is directed to 'http://example.com/user/7854' (for example) The mod_rewrite regex will translate via apache to my php script: 'http://example.com/user.php?id=7854' because anything after 'user/' becomes captured. That being said, this is my user.php:

<?php
    echo $_GET['id'];
?>

now as you can imagine 'http://example.com/user.php?id=7854' displays 7854 however 'http://example.com/user/7854' gives me an error (404) why is this? what have i misunderstood? I've spent an hour on numerous forums just to get this far, this concept isnt coming easy to me at all. cheers to any help.

Upvotes: 0

Views: 39

Answers (2)

hieroshima
hieroshima

Reputation: 242

Like @andrea.r says: you have rule for all characters but no numbers.

Change: RewriteRule ^user/([a-z]+)/$ user.php?id=$1

to: RewriteRule ^user/([a-z,0-9]+)/$ user.php?id=$1

Upvotes: 2

Andrea Riva
Andrea Riva

Reputation: 166

User ID is numerical and you've written [a-z] instead of [0-9].

Try this: RewriteRule ^user/([0-9]+)/?$ /user.php?id=$1

Upvotes: 0

Related Questions