Redirection Examples

A simple example:
RewriteLogLevel 3
RewriteLog c:\temp\iirf
StatusUrl /iirfStatus

# --------
# Any incoming URL that specifies a server name other than server.domain.com
# and that has a URL path that starts with an uppercase W
# will be redirected to the specified server.
# Ex: http://example.com/Warehouse will redirect (302) to http://server.example.com:8080/Warehouse . 

RewriteCond   %{HTTP_HOST} ^(?!server\.domain\.com)$
RedirectRule  ^/(W.*)$  http://server.example.com:8080/$1  


Here's another example:
RewriteLogLevel 3
RewriteLog c:\temp\iirf
StatusUrl /iirfStatus

# ------
# Any incoming URL request that specifies domain.com 
# will be redirected to the www.domain.com server.

RewriteCond  %{HTTP_HOST} ^domain\.com$
RedirectRule ^/(.*)$        http://www.domain.com/$1   


This is the opposite:
RewriteLogLevel 3
RewriteLog c:\temp\iirf
StatusUrl /iirfStatus

# ------
# Any incoming URL request that specifies www.domain.com 
# will be redirected to the domain.com server.

RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RedirectRule ^/(.*)$        http://domain.com/$1   


Here's one that redirects to a secure site when necessary:
RewriteLogLevel 3
RewriteLog c:\temp\iirf
StatusUrl /iirfStatus

# ------
# Any incoming URL request that is not using HTTPS ,
# and is an admin URL, 
# will be redirected to the secure site.

RewriteCond %{HTTPS} ^off$
RedirectRule ^/admin/(.*)$        https://domain.com/admin/$1   



Here's one that redirects based on the user-agent :
RewriteLogLevel 3
RewriteLog c:\temp\iirf
StatusUrl /iirfStatus

# ------
# Any incoming URL request that has User-Agent that includes the string "Mobile",
# will be redirected.

RewriteCond %{USER_AGENT} Mobile
RedirectRule ^(.*)$        http://domain.com/mobile/$1   




Here's one that redirects to a secure site, using conditions on the HTTP_HOST as well as the HTTPS variable:
RewriteLogLevel 3
RewriteLog c:\temp\iirf
StatusUrl /iirfStatus

# ------
# Any incoming URL request that specifies secure.domain.com 
# (which is configured as a DNS alias), and is not using SSL, will
# will be redirected to the secure server.  The RewriteCond statements
# implicitly "chain" with a logical AND. 

RewriteCond  %{HTTP_HOST} ^secure\.domain\.com$
RewriteCond  %{HTTPS}       ^off$
RedirectRule ^/(.*)$        https://secure.domain.com/$1   


Last edited Jul 24, 2009 at 4:54 PM by Cheeso, version 8

Comments

codex73 Sep 5, 2008 at 9:25 PM 
what if we need to redirect the url to an https one and it also has the www?