Direct all traffic to www.domain.com, unless on dev server

New to mod_rewrite? This is a good place to start.

Direct all traffic to www.domain.com, unless on dev server

Postby Marc_J » Sun Jul 19, 2009 9:43 am

I've got a website with multiple domains parked on it, and I use the following in .htaccess to direct all traffic to www.domain.com: -

Code: Select all
# Direct all traffic to domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*) http://www.domain.com/$1 [L,R=301]


The trouble is I now develop on a local server before uploading to my remote server, and the above redirect is obviously causing problems when I try to view the local version. The local server is on a NAS server and is accessed via http://devserver/sitename/

Is there any way I can use the same .htaccess on both the local and remote versions of the site, with the redirect only working on the remote version? Maybe another condition to check that the HTTP_HOST doesn't include "devserver" before redirecting?
Marc_J
 
Posts: 25
Joined: Tue Aug 15, 2006 9:00 am

Postby richardk » Sun Jul 19, 2009 5:32 pm

Maybe another condition to check that the HTTP_HOST doesn't include "devserver" before redirecting?

Yes.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteCond %{HTTP_HOST} !^devserver$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Marc_J » Sun Jul 19, 2009 11:32 pm

Thanks very much! I thought it would be that simple, but didn't want to mess around on a live site.

Is the Options +FollowSymLinks really necessary? In an .htaccess file deeper in the site I have: -

Code: Select all
RewriteEngine on

Options +SymlinksIfOwnerMatch +MultiViews
RewriteRule ^(.*).php/(.*) $1.php?$2


Will your edit have any effect on this?

Also, in the root .htaccess, I also have: -

Code: Select all
# Define the location of my error documents
ErrorDocument 404 http://www.domain.com/page/error404


Since I don't want any local errors sending me to the remote error page, is there a way to also make this conditional, with dev errors going to http://devserver/sitename/page/error404 instead?
Marc_J
 
Posts: 25
Joined: Tue Aug 15, 2006 9:00 am

Postby richardk » Mon Jul 20, 2009 4:35 pm

Is the Options +FollowSymLinks really necessary?

Not if it works without it.

In an .htaccess file deeper in the site I have: -
Code: Select all
RewriteEngine on

Options +SymlinksIfOwnerMatch +MultiViews
RewriteRule ^(.*).php/(.*) $1.php?$2

Will your edit have any effect on this?

It might make Apache follow all Symbolic Links instead of just the ones it owns.

Also
Code: Select all
RewriteRule ^(.+)\.php/(.*)$ $1.php?$2 [QSA,L]


Also, in the root .htaccess, I also have: -
Code: Select all
# Define the location of my error documents
ErrorDocument 404 http://www.domain.com/page/error404

Since I don't want any local errors sending me to the remote error page, is there a way to also make this conditional, with dev errors going to http://devserver/sitename/page/error404 instead?

You could define a variable in the dev servers configuration (the httpd.conf file would probably be best) with SetEnv
Code: Select all
SetEnv DEV_SERVER true

and then used <IfDefine> to do things based on if it is set or not
Code: Select all
<IfDefine DEV_SERVER>
  ErrorDocument 404 http://devserver/sitename/page/error404
</IfDefine>

<IfDefine !DEV_SERVER>
  ErrorDocument 404 http://www.domain.com/page/error404
</IfDefine>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Marc_J » Tue Jul 21, 2009 3:34 am

Thanks again.

I added
Code: Select all
SetEnv DEV_SERVER true

to the dev server's apache.conf, and in .htaccess used
Code: Select all
<IfDefine DEV_SERVER>
  ErrorDocument 404 http://devserver/sitename/page/error404
</IfDefine>

<IfDefine !DEV_SERVER>
  ErrorDocument 404 http://www.domain.com/page/error404
</IfDefine>

as you suggest, then restarted apache, but the error pages still went to the remote server (http://www.domain.com/page/error404). Any idead why this might be happening?
Marc_J
 
Posts: 25
Joined: Tue Aug 15, 2006 9:00 am

Postby Marc_J » Tue Jul 21, 2009 8:33 am

Further info - if I view a page with the following PHP on the dev server: -

Code: Select all
<?php
echo $_SERVER[DEV_SERVER];
?>


The output is "true".

So it is definitely set correctly, but the condition in .htaccess is somehow not working....
Marc_J
 
Posts: 25
Joined: Tue Aug 15, 2006 9:00 am

Postby richardk » Tue Jul 21, 2009 12:33 pm

My mistake, <IfDefine> does not work on environmental variables
http://httpd.apache.org/docs/2.2/mod/core.html#ifdefine wrote:Syntax: <IfDefine [!]parameter-name> ... </IfDefine>


The parameter-name argument is a define as given on the httpd command line via -Dparameter- , at the time the server was started.


So you can either do that, or do something with mod_rewrite like
Code: Select all
RewriteCond %{HTTP_HOST} ^devserver$ [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule .* http://devserver/sitename/page/error404 [R,L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule .* http://www.domain.com/page/error404 [R,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Marc_J » Fri Jul 24, 2009 7:55 am

Thanks again - I added the -D parameter at startup on the dev server and everything works as expected :)
Marc_J
 
Posts: 25
Joined: Tue Aug 15, 2006 9:00 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 21 guests

cron