Examples - Practical uses
Making dynamic sites look static.
Lets take for example you have an online shop and you want all your products to be indexed on search engines, but because you use long dynamic URLs they aren't.
With Mod_Rewrite you can change the URLs of your site to look static making it easier for search engines to crawl your site.
An example mod_rewrite.ini for the example above looks like this
----------
1: Debug 0
2: Reload 1000
3: RewriteRule ^/product-(.*)-(.*).htm /product.asp?id=$1&catid=$2
----------
Now any request to your site like http://www.yoursite.com/product-100-1.htm will be replaced in the background to http://www.yoursite.com/product.asp?id=100-1.
Mod_Rewrite uses regular expressions to match incoming URLs, meaning you can make your website's URLs to look exactly the way you want. For more information please contact us using our online form.
Stopping E-Mail Crawlers from browsing your site.
Example mod_rewrite.ini
----------
1: Debug 0
2: Reload 1000
3: RewriteCond HTTP_USER_AGENT EmailExtractor
4: RewriteRule ^/(.*) /fakeemails.asp
----------
With this rule you are telling the filter: If any bot or browser with EmailExtractor in the user-agent tag asks for any url (.*) send them to /fakeemails.asp
Emulating VirtualHosting with Mod_Rewrite
Example mod_rewrite.ini
----------
1: Debug 0
2: Reload 1000
3: RewriteCond HTTP_HOST www.mysite1.com
4: RewriteRule ^/(.*) /site1$1
5: RewriteCond HTTP_HOST www.mysite2.com
6: RewriteRule ^/(.*) /site2$1
----------
With this rule you are telling the filter: If someone wants to access mysite1.com redirect any url (.*) to my subfolder /mysite and append the URL to /mysite
This way an url like www.mysite1.com/helloworld.asp would be redirected to /mysite1/helloworld.asp.
Creating dynamic subdomains
Example mod_rewrite.ini
----------
1: Debug 0
2: Reload 1000
3: RewriteCond HTTP_HOST (.*)\.yourdomain\.com
4:
RewriteRule (.*) /default.asp?site=$C&loc=$1
----------
Note: $C is case sensitive
Now if you type host1.yourdomain.com/helloworld.asp
The filter will rewrite the URL to /default.asp?site=host1&loc=/helloworld.asp
Creating permanent redirections 301
Example mod_rewrite.ini
----------
1: Debug 0
2: Reload 1000
3: RewriteRule ^/olddir/(.*) /newdir/$1 [r=301]
----------
Note: it is also possible to create 302 redirects, just change 301 with 302
Different browser different content
Example mod_rewrite.ini
----------
1: Debug 0
2: Reload 1000
3: RewriteCond HTTP_USER_AGENT MSIE
4:
RewriteRule (.*) /ie$1
5: RewriteCond HTTP_USER_AGENT Firefox
6: RewriteRule (.*) /ff$1
----------
With this rule you are telling the filter: If someone is using a browser with the user-agent tag msie on it, redirect them to the subfolder /ie/.
This way someone requesting an url /helloworld.asp with Internet Explorer will be redirected /ie/helloworld.asp.
Stop people from linking to your files, images and wasting your bandwidth.
Example mod_rewrite.ini
----------
1: Debug 0
2: Reload 1000
3: RewriteCond HTTP_REFERER theirsite.com [OR]
4: RewriteCond HTTP_REFERER thersite1.com
5: RewriteRule
.zip /denied.htm
----------
With this rule you are telling the filter: If someone with theirsite.com or theirsite1 as referer is asking for a gif or zip file, send them to /denied.htm.
|