URL Rewriting – An Introduction

1. Rewriting all URLs ENDING with “my-string” and sending it to my-file.php

$ indicates end of string.
i.e. match any URI ending with string preceding $

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /my-string$
RewriteRule ^([_a-zA-Z0-9-]+)$ /my-file.php?string=$1 [L,NC]

2. Rewriting all URLs ENDING with “my-string” OR “another-string” and sending it to my-file.php

RewriteCond %{REQUEST_URI} /my-string$| /another-string$
RewriteRule ^([_a-zA-Z0-9-]+)$ /my-file.php?string=$1 [L,NC]

3. Rewriting all URLs matching this pattern
my-string/(any-alpha-numeric-chars)/(any-alpha-numeric-chars)/

RewriteCond %{REQUEST_URI} /event/([_a-zA-Z0-9-]+)/([_a-zA-Z0-9-]+)$
RewriteRule ^([_a-zA-Z0-9-]+)$ /my-file.php?string1=$1&string2=$2&string3=$3 [L,NC]

This entry was posted in URL Rewriting. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *