Rewriting URLs with mod_rewrite

0 votes
asked Jun 9, 2010 by webby

I'm currently rewriting urls from

this to this

with the following rewrite

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\/?$ index.php?u=$1 [L]

However I'd like to optimize for seo a litte and go with :

Obviously the /Anything-I-want-here is just null ignored ...

Any idea's guys? much appreciated

1 Answer

0 votes
answered Jun 9, 2010 by zildjohn01

Simply remove the $ from the regex, and anything after the ID number will be ignored, and the URL will be rewritten correctly.

RewriteRule ^(.*?)\/? index.php?u=$1 [L]

# the following will work the same (as far as I can tell), and
# it's a lot more obvious at first glance what it does, which is
# match everything until the first slash
RewriteRule ^([^/]+)     ...

When I do something like this, I like to verify the URL in code, and 301 redirect if the "Anything-I-want-here" doesn't match the data.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter