Forum Moderators: phranque

Message Too Old, No Replies

Two part www and domain typo redirect script

Is this working script able to be better optimized?

         

otem

1:44 am on Feb 21, 2007 (gmt 0)

10+ Year Member



Hello,

I'm working on touching up a domain redirect script I have, hoping somebody might be able to give some improvement tips:

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule (.*) [www\.domain\.com...] [R=301,L]

RewriteCond %{HTTP_HOST}!^www\.domain\.com$ [NC]
RewriteCond $1!^typo-domain\.php$
RewriteRule (.*) /typo-domain.php?url=$1 [L]

Two parts to the script. The first part is to redirect from non-www version of a domain to the www version of the domain.

The second part of the script looks to see if it is the www version (which it now should be if was the non-www version of the domain name), else, send the visitor to a special script can store that domain to a database and to 301 redirect them from there to the correct page. (I'm using that to be able to count typo variants of my domain.)

Is this the best way to do this? Seems kind of bulky.

Originally I had this as it was either the correct domain with www, or it was redirected to my script. Seemed kind of wasteful for people who just forgot the www section.

jdMorgan

2:19 pm on Feb 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Te only change I would suggest is either omitting the "$" end-anchor from the domain names in your RewriteConds, or allowing for an appended port number. That is, either

RewriteCond %{HTTP_HOST} ^domain\.com [NC]
-or-
RewriteCond %{HTTP_HOST} ^domain\.com(:80)?$ [NC]
-or-
RewriteCond %{HTTP_HOST} ^domain\.com(:[0-9]{1,5})?$ [NC]

Jim