mod-rewrite Basic internal rewrites Fancy url to php script


Example

In this example, we rewrite url's of the form http://example.com/topic/id-seoname to a php script that takes an id as input. This example expects the rule to be in "per-directory" context.

RewriteEngine on

RewriteRule ^topic/([0-9]+)-[^/]*/?$ /topics.php?id=$1 [L]

In this example, topic/ is the common prefix of all topics. It is followed by a number that is used by the script. Lastly, the seo name is displayed. This seo name is ignored by mod_rewrite, because it is only there for seo reasons. The second argument of RewriteRule contains the url to rewrite to. The placeholder $1 is replaced with the content of the first capture group in the regex before it. In this case it will be replaced with what is matched with ([0-9]+).