Apache Rewrites can be handy in various scenarios. In order to use apache  rewrite you have to have the mod_rewrite enabled.

Syntax:  RewriteRule  [Optional Flags]

<Directory /var/www/html/sample/> RewriteEngine On RewriteRule ^(test)/([0-9A-Za-z-]+)$   test.php?id=$2  [L]

This will redirect url/test/53xml to url/test.php?id=53xml

Optional Flags:

F : Forbidden , L : Last rule, no other rules will be processed if this matches, R: Redirect , the users url will be visibly redirected to the substitution. You should make a true url in this case.    NC: means no case (ignore case)

Example 1:  Create a page named goto.php which takes a parameter name. So that goto.php?name=main would take to main page. So a url name/main would take to goto.php?name=main. Now lets see the rule:

RewriteRule ^name/([^/.]+)/?$   goto.php?name=$1 [L]

^name/ : Checks if the requested url starts with name/

([^/.]+) : Closing brackets means that anything matched will be remembered for use with $ variable. Inside the bracket it means, one or more characters that are not a forward slash or a period. Anything found will be matched and remembered into $1

/?$ : Checks that only thing found after what was matched is just a forward slash.

goto.php?name=$1 : Page to be loaded, $1 = matched variable

Avoiding Hotlinked images with Rewrite:

RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www.)?somesite.com/.*$ [NC] RewriteRule .(gif|jpg|png)$ http://www.somesite.com/nasty.gif [R,L]

You can use  .(gif|jpg|png)$ – [F]. to forbid the image. The RewriteCond is the condition and passes down if the first one matches.

Creating a default page for gallery:

RewriteCond %{QUERY_STRING} ^pageId=0&start=0$ 
RewriteRule ^gallery/index.php gallery/index.php?pageId=117&start=0 ```

# Redirect all pages from olddomain.com # to newdomain.com Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} ^www.olddomain.com$[OR] RewriteCond %{HTTP_HOST} ^olddomain.com$ RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L] #Prevent subfolder loading. This goes # in htaccess for the primary domain RewriteCond %{HTTP_HOST} ^primary\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.primary\.com$ RewriteRule ^addon\.com\/?(.*)$ "http\:\/\/www\.addon\.com\/$1" [R=301,L] #Prevent subdomain name loading. #This goes in htaccess for the primary domain RewriteCond %{HTTP_HOST} ^subname\.primary\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.subname\.primary\.com$ RewriteRule ^(.*)$ "http\:\/\/www\.addon\.com\/$1" [R=301,L] # Never use www in the domain # Replace 'example.com' with your domain name RewriteEngine on RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?example\.com)$ [NC] RewriteRule .? http://%1%{REQUEST_URI} [R=301,L] # Always use www in the domain # Replace 'example.com' with your domain name RewriteEngine on RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC] RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule .? http://www.%1example.com%{REQUEST_URI} [R=301,L] # Set a default home directory, (this subfolder always loads) # Replace 'folder' with your subfolder name <ifmodule mod_rewrite.c=""> RewriteEngine On RewriteRule ^$ /folder/ [R=301,L] </ifmodule> # Rename a directory and force visitors to the new name # Replace 'old' with your old folder name # Replace 'new' with your new folder name RewriteEngine on RewriteRule ^/?old([a-z/.]*)$ /new$1 [R=301,L] # Always use https for secure connections # Replace 'www.example.com' with your domain name # (as it appears on your SSL certificate) RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L] # Block traffic from multiple referrers RewriteEngine on Options +FollowSymlinks RewriteCond %{HTTP_REFERER} badsite\.com [NC,OR] RewriteCond %{HTTP_REFERER} badforum\.com [NC,OR] RewriteCond %{HTTP_REFERER} badsearchengine\.com[NC] RewriteRule .* - [F]