Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 308
Frog Avatar asked Sep 17 '26 05:09

Frog


1 Answers

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]
like image 104
Michael Berkowski Avatar answered Sep 18 '26 19:09

Michael Berkowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!