Mod_rewrite solved problems

Basic examples

Permanent move to new domain

Use these lines if you want move example.com to new domain example-new.com.

# move example.com to example-new.com
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example-new.com/$1 [R=301,L]

Use subdirectory as website root

Content located in example.com/subdir/ will be on your domain root. If you go to example.com you'll get index of subdir/ directory.

RewriteCond %{REQUEST_URI} !subdir/
RewriteRule (.*) http://example.com/subdir/$1 [L]

Nice URL for searching

If you want to have URL for every site search, you can archieve this with this one line of code:

RewriteRule ^/search/(.*)$ /search.php?query=$1 [PT]

And you'll have /search/query url.

Cool URI for articles

If you have /article.php?id=11 in old publication system and do you want to have something like /article/11 use this:

RewriteRule ^/article/([0-9]+)$ /article.php?id=$1 [PT]

Simple redirect (dir to file)

If you want technology independent URIs you can use this rules.

RewriteRule ^/about-us/ /about-us.php [L]

Advanced examples

Domain 301 redirect

To redirect one domain local.example.biz to another example.com use this:

RewriteCond %{HTTP_HOST} local\.example\.biz
RewriteRule ^(.*) http://example.com/$1 [R=301,L]

Get 3rd level domain as parameter

Get all subdomain names as domain parameter except www. prefix.

RewriteCond %{HTTP_HOST} !^(www\.)?example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([\.]+)\.example.com$ [NC]
RewriteRule .* /process.php?domain=%1 [L]

Extension to parameter

To process article.12 to article.php?id=12 use this:

RewriteRule ^article\.([0-9]+) article.php?id=$1 [QSA,L]

Thanks to QSA you'll get all params after path /article.12

Other mod_rewrite constructions

If these lines didn't solved your problems, you can use other resources for mod_rewrite: