Apache Rewrite Rules can be used to transform a pretty ugly looking URL into a beautiful, SEO-friendly and meaningful URL. I would like to list a couple of real-world examples below to explain how Rewrite Rules can be set up and how they work. Rewrite Rules use Regular Expressions, so this article is closely related to my Regular Expressions article.
Time-Dependent Rewriting
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900
RewriteRule ^test\.html$ test.day.html [L]
RewriteRule ^test.html$ test.night.html
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900
RewriteRule ^test\.html$ test.day.html [L]
RewriteRule ^test.html$ test.night.html
Redirect Failing URLs to Another Web Server
RewriteCond /your/docroot/%{REQUEST_FILENAME} !-f
RewriteRule ^(.+) http://webserverB.com/$1 [R]
RewriteRule ^(.+) http://webserverB.com/$1 [R]
Browser Dependent Content
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/3.*
RewriteRule ^foo\.html$ foo.NS.html [L]
RewriteCond %{HTTP_USER_AGENT} ^Lynx/.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[12].*
RewriteRule ^foo\.html$ foo.20.html [L]
RewriteRule ^foo\.html$ foo.32.html [L]
RewriteRule ^foo\.html$ foo.NS.html [L]
RewriteCond %{HTTP_USER_AGENT} ^Lynx/.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[12].*
RewriteRule ^foo\.html$ foo.20.html [L]
RewriteRule ^foo\.html$ foo.32.html [L]
Blocking of BAD Proxied IP Addresses
RewriteMap hosts-deny txt:/etc/apache2/conf/host.deny
RewriteCond ${hosts-deny:%{HTTP:True-Client-IP}|NOT-FOUND} !=NOT-FOUND
RewriteRule ^/.* - [R=401,L]
RewriteCond ${hosts-deny:%{HTTP:True-Client-IP}|NOT-FOUND} !=NOT-FOUND
RewriteRule ^/.* - [R=401,L]
Redirecting a URI to a New Format
RewriteCond %{QUERY_STRING} !marker
RewriteCond %{QUERY_STRING} id=([-a-zA-Z0-9_+]+)
RewriteRule ^/?human\.php$ /%1? [R,L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ human.php?marker&id=$1 [L]
RewriteCond %{QUERY_STRING} id=([-a-zA-Z0-9_+]+)
RewriteRule ^/?human\.php$ /%1? [R,L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ human.php?marker&id=$1 [L]
Force some pages to use Secure Server
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/?(page1|page2|page3|page4|page5)$ https://www.secure-server.com/%1 [R,L]
RewriteRule ^/?(page1|page2|page3|page4|page5)$ https://www.secure-server.com/%1 [R,L]
Redirect UserAgent for IPAD, IPHONE, And Anroid OS
RewriteCond %{HTTP_USER_AGENT} !^.*iPhone.*$ [NC]
RewriteCond %{HTTP_USER_AGENT} !^.*iPad.*$ [NC]
RewriteCond %{HTTP_USER_AGENT} !^.*Android.*$ [NC]
RewriteCond %{REQUEST_URI} ^/tab
RewriteCond %{HTTP_USER_AGENT} !^.*iPad.*$ [NC]
RewriteCond %{HTTP_USER_AGENT} !^.*Android.*$ [NC]
RewriteCond %{REQUEST_URI} ^/tab
RewriteRule .* /index.html [R=301,L]
Block Bad Referrer
RewriteCond %{HTTP_REFERER} (tvchannelsfree) [NC,OR]
RewriteCond %{HTTP_REFERER} (onionz) [NC]
RewriteRule ^/.* - [R=401,L]
RewriteCond %{HTTP_REFERER} (onionz) [NC]
RewriteRule ^/.* - [R=401,L]
Make Fancy URLS
Ex. What to What:
http://domain.com/index.php?id1=3456&text=humanbeing_nonsense&sub_id=14 TO
http://domain.com/senses/humanbeing_nonsense/3456/14.html
RewriteRule ^senses/([a-z_]+)/([0-9]+)/([0-9]+)\.html$ /index.php?id1=$2&text=$1&sub_id=$3
Redirect Page Request to Other Server
RewriteCond %{REQUEST_URI} /username/register.php
RewriteRule .* http://domain2.com/username/register_new.php [R=301,NC]
RewriteCond %{REQUEST_URI} /username/entry.html
RewriteRule .* http://domain2.com/username/entry.html [R=301,NC]
RewriteRule .* http://domain2.com/username/register_new.php [R=301,NC]
RewriteCond %{REQUEST_URI} /username/entry.html
RewriteRule .* http://domain2.com/username/entry.html [R=301,NC]
No comments:
Post a Comment