Examples

Example 1-31. Simple match

# Rewrite /foo to /bar
RewriteEngine On
RewriteRule ^/foo$ /bar

Example 1-32. Match and capture group

# Rewrite pretty url as script parameters
RewriteRule ^/(\w+)/(\d+) /index.php?action=$1&id=$2

Example 1-33. Rewrite conditions

# Limit admin url to internal IP addresses
RewriteCond %{REMOTE_ADDR} !192.168.\d*.\d*
RewriteCond %{PATH_INFO}  ^admin
RewriteRule .* - [F]

Example 1-34. Redirect to SSL

# Make sure admin urls are served over SSL
RewriteCond   %{SERVER_PORT}  !^443$
RewriteRule ^/admin/(.*)$ https://www.example.com/admin/$1
[L,R]

Get Regular Expression Pocket Reference, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.