mod-rewrite

Basic internal rewrites

Fancy url to php script

In this example, we rewrite url’s of the form https://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]+).

Url with query string to php script

To match a query string, a condition must be added to the RewriteRule. This is done by putting RewriteCond directives before the corresponding rule. In the following example we dynamically internally rewrite an old url to a new url.

RewriteCond %{QUERY_STRING} ^name=([^&]*)$
RewriteRule ^oldscript\.php$ newscript.php?username=%1 [L]

Please note that to match the literal dot, we have to escape it with a slash. %1 is replaced with the first capture group of the previous condition. In this case it is replaced by whatever is matched by ([^&]*).


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow