Mod_rewrite solved problems

Basic examples

Permanent move to new domain

Use these lines if you want move to new domain

# move to
RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

Use subdirectory as website root

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

RewriteCond %{REQUEST_URI} !subdir/
RewriteRule (.*)$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 to another use this:

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

Get 3rd level domain as parameter

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

RewriteCond %{HTTP_HOST} !^(www\.)?$ [NC]
RewriteCond %{HTTP_HOST} ^([\.]+)\$ [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: