Forum Moderators: phranque

Message Too Old, No Replies

Generic www. redirect and limiting where it happens

htaccess generic www. redirect rewritecond turn off for development server

         

troyinexo

10:09 pm on Apr 12, 2010 (gmt 0)

10+ Year Member



Hello everyone, hope everyone is having a great day. I am in need of some assistance from a htaccess guru, or at least a point in the right direction.

Here the short of it:

We have a custom php framework used on client sites and I am trying to set up a generic htaccess piece to redirect all the domains to a www. version so we don't have to set one up individually for each site. The caveat is that this framework runs on development and staging, as well as the live production servers, so when we are on development, say dev.example.com or demo20.example.com, it should NOT forward to the www. (as this results in a broken page).

Below I've copied what I have come up with so far, but this first rewritecond doesn't work, keeps going to www.dev.example.com.

I appreciate all replies.


RewriteCond %{HTTP_HOST} !^http://(dev|demo\d{1,})(.*?).(biz|com|edu|gov|info|jobs|mil|name|net|org|pro|us)(.*?) [NC]

RewriteCond %{HTTP_HOST} ^(.*?)\.(biz|com|edu|gov|info|jobs|mil|name|net|org|pro|us) [NC]

RewriteRule ^(.*)$ http://www.%1.com/$1 [L,R=301]

g1smd

12:44 am on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You don't need http:// in the RewriteCond.

I'm looking at the code, and I have to believe there's a more efficient way of coding it, but since it is nearly 1 a.m. here that'll have to wait.

jdMorgan

1:05 am on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This will probably cover all of the TLDs you'd need in almost every case. It also takes care of FQDNs and appended port numbers.

RewriteCond %{HTTP_HOST} !^(www|dev|demo[0-9]+)\.[^.]+\.(com(\.[a-z]{2})?|co\.[a-z]{2}|[a-z]{2,6})\.?(:[0-9]{1,5})?$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.(com(\.[a-z]{2})?|co\.[a-z]{2}|[a-z]{2,6}))\.?(:[0-9]{1,5})?$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

Things get complicated, though, if you need to handle HTTPS with the same rule... :)

That TLD subpattern handles all of .com, .com.ca, .co.uk, .info, .museum, etc.

Jim

[edited by: jdMorgan at 2:06 am (utc) on Apr 17, 2010]

troyinexo

4:24 pm on Apr 13, 2010 (gmt 0)

10+ Year Member



jdMorgan,

I can only imagine how it could become complicated with secured connections. Is it because usually only certain urls are secured? Couldn't I just override the mod_rewrite for those certain cases when I need to use https? I am just trying to understand the implications.

-Troy

troyinexo

6:17 pm on Apr 16, 2010 (gmt 0)

10+ Year Member



Also, can you explain some of the directive, in particular (com(, it looks like that would need an explicite com (no modifiers attached) is there something in the syntax that I don't understand?

g1smd

7:31 pm on Apr 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It allows for
.com.uk
and other such TLDs, both with and without appended port numbers.

jdMorgan

2:10 am on Apr 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"com(\.[a-z]{2})?" matches "com.ca" or "com.au" or just "com" -- The parenthesized sub-expression is quantified with the "zero or one" regex quantifier "?" and is therefore optional.

Adding support for HTTPS isn't all that difficult. I just didn't want to complicate the example code if it wasn't needed. The simplest way is to just use two rules, one for HTTP and one for HTTPS, testing %{SERVER_PORT} =443 (HTTPS) or %{SERVER_PORT} !=443 (HTTP) and redirecting accordingly.

Jim