Frog
Frog

Reputation: 1641

Apache mod_rewrite: what am I doing wrong?

I've got a really simple rewrite in my .htaccess file, but it doesn't exactly work the way I want. This is my code:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^admin/?$ admin.php [L]
    RewriteRule ^([0-9]*).*$ index.php?id=$1 [L]
</IfModule>

Basically what I want is that every page is like this: /0/title. The title is just to make the URL clearer for the user, but the number (id) should be passed to my PHP script. With this code the id is not passed to my index.php script. It is passed to that script if I just remove ".*" from the fourth row, but then URL's with text after the number don't get passed to my index.php file.

What am I doing wrong? How can I fix this?

Thanks!

Upvotes: 3

Views: 105

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270637

You are trying to trap URL's like /0/title, but you don't have a slash / in your match pattern. Try this instead:

# Should match /01234/anything
# with the "/anything" optional
RewriteRule ^([0-9]+)(/.*)?$ index.php?id=$1 [L]

Upvotes: 4

Related Questions