Apache rewrite sample rules

Example Rules

domain.com permanently moved to domain2.com

 RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
 RewriteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L]

domain.com/page.html moved to domain.com/new_page.html temporarily

  RewriteRule ^page.html$ new_page.html [R,NC,L]

Nice looking URLs (no querstring)                                                                                                                                                        eg. domain.com/category-name-1/ to domain.com/categories.php?name=category-name-1

 RewriteRule ^([A-Za-z0-9-]+)/?$ categories.php?name=$1 [L]

Nice looking URLs ( no query string with pagination )

eg. domain.com/articles/title/5 to domain.com/article.php?name=title&page=5

 RewriteRule ^articles/([A-Za-z0-9-]+)/([0-9]+)/?$ article.php?name=$1&page=$2 [L]

Block referrer spam

RewriteCond %{HTTP_REFERRER} (weight) [NC,OR] RewriteCond %{HTTP_REFERRER} (drugs) [NC] RewriteRule .* - [F]

Rewrite To a Folder with Folder Name Hidden in the URL and skip some folders :

RewriteCond    %{SERVER_NAME}   ^domain.com                           [NC,OR]
RewriteCond    %{SERVER_NAME}   www.domain.com                        [NC]
RewriteCond   %{REQUEST_URI}    !/?(folder1toskip | folder2toskip)/   [NC]
RewriteCond   %{REQUEST_URI}    !^/foldertoredirectto/                [NC]
RewriteRule   (.*)              /foldertoredirectto/$1                [L]

You may also like...