Posted by CANbike on Sun, 6 Jul 2014

[.htaccess] 301 Redirect with OR and NC

canbike.ca has moved to canbike.org. However, to ease the transition, old URL links should be redirected to the new domain with a server-side 301 “Moved Permanently” response.

Attempt 1: A Simple 301 Redirect

This first attempt was added to the .htaccess file in the root folder for CANbike.ca.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !canbike.org$ [NC]
RewriteRule ^(.*)$ http://www.canbike.org/$1 [L,R=301]

If the pattern does not start with canbike.org, [NC] case insensitive, then 301 redirect to http://www.canbike.org/ along with any additional parameters $1.

Attempt 2: With OR Without www

Attempt “1” worked well for all cases. But for custom reasons, a more specific and limited rewrite condition was required.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^canbike.ca [NC,OR]
RewriteCond %{HTTP_HOST} ^www.canbike.ca [NC]
RewriteRule ^(.*)$ http://www.canbike.org/$1 [L,R=301]

If the [NC] case insensitive pattern starts with canbike.ca OR www.canbike.ca, then 301 redirect to http://www.canbike.org/ along with any additional parameters $1.

In other words, www.canbike.ca/bike.html will redirect to www.canbike.org/bike.html.

htaccess 301 redirect to new domain-thumb.jpghtaccess 301 redirect with OR without www case insensitive to new domain-thumb.jpg


Related Item(s):