Forum Moderators: phranque

Message Too Old, No Replies

htaccess redirect and/or rewrite failing

         

gkarracer

1:42 am on May 27, 2010 (gmt 0)

10+ Year Member



On the simple side I'm trying to redirect (or rewrite) everything from non-www to www. I found sample code (see below), but it doesn't work most of the time.

The base domain is one server/IP address, while "www" is on a different server/IP address. I would like requests to example.com to auto forward to www.example.com so it points to the correct server. The content on the "www" server does not exist on the base server, but there is an old website that we are migrating from (with different content).

I've tried various sample code, but the redirect only happens when I specify a URL that matches a file on the base server. Nothing is redirected otherwise.

For example, here is code I tried that rewrites base URL to www:

RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

This only works with a URL such as: example.com/avalidfileonoldserver.htm, but does not work with
example.com or example.com/fileonlyonnewserver.htm

In case it matters, the old server is a ruby on rails application.

Any ideas?

Thanks.

jdMorgan

3:58 am on May 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try disabling MultiViews (content negotiation) and AcceptPathInfo as a first step.

Both of these features can "rewrite" non-existent URL requests, and will take precedence over mod_rewrite.

The directives would be:

Options -MultiViews
AcceptPathInfo Off

Note that AcceptPathInfo is supported only on Apache 2.0 and later.

If this doesn't help, review the server config files for any directives which may be pre-empting your rewriterule.

Jim

gkarracer

5:35 pm on May 27, 2010 (gmt 0)

10+ Year Member



Unfortunately it didn't work. Thanks for the suggestion. I don't think I have access to the server config files. I don't control the Apache server. This is an account on a general hosting company. I do have SSH access so I can see any files in my section. If there's something in there I should look for let me know. Otherwise any configuration is via cPanel X.

Any other ideas?

g1smd

9:34 pm on May 27, 2010 (gmt 0)

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



In what way did it not work?

The redirect code is some of the most basic code you could ever add to the server configuration.

gkarracer

10:00 pm on May 27, 2010 (gmt 0)

10+ Year Member



It works only with known valid files on the initial server. In order to make it clear:

The domain (example.com) URL points to ServerA and "www" points to ServerB.
Let's say pagea.html is a valid page on ServerA and pageb.html is a valid page on ServerB. pageb.html is NOT a valid page on ServerA and pagea.html is NOT a valid page on ServerB.

Using the code I listed in the initial post, the following URLs have these actions:

  • www.example.com - pulls up default page on ServerB as expected (rewrite rule is not applicable).
  • www.example.com/pageb.html - pulls up pageb.html on ServerB as expected (rewrite rule is not applicable).
  • example.com - pulls up default page on ServerA (incorrect!).
  • example.com/pagea.html - browser switches to www.example.com/pagea.html and fails to load (invalid page on ServerB, but valid on ServerA). This is the correct action.
  • example.com/pageb.html - fails to load (invalid page on ServerA) and browser keeps example.com/pageb.html in address bar (incorrect!).

g1smd

11:19 pm on May 27, 2010 (gmt 0)

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



Ah, you need for your non-www to www redirect to kick in only if the file does not exist on serverA. The original question did not make that clear.

Your original redirect was unconditional. You'll need a couple more lines of code, namely at least a -f "exists" check in a preceding RewriteCond.

jdMorgan

12:15 am on May 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> known valid files

This is the crux of the matter. Somewhere in your config, in your .htaccess file(s), or in your scripts, some "agent" is testing for "known valid files" and pre-empting your .htaccess code.

This is not a 'core' server function.

So, the task is to identify this agent and disable it or modify it.

MultiViews (mod_negotiation) and AcceptPathInfo were the first two candidates. Others are mod_alias, mod_speling, mod_proxy -- and several others. Directives processed by these modules are "aware" of the "exists/does not exist" status of resources.

You might want to 'watch' your server responses to the requests you listed above, using something like the "Live HTTP Headers" add-on for Fireofox to be sure that you're not getting multiple/stacked/chained redirects. So far, we're proceeding on the assumption that "everything goes wrong all at once in one step," but that might not be the case.

Jim

gkarracer

4:51 pm on May 28, 2010 (gmt 0)

10+ Year Member



Ah, you need for your non-www to www redirect to kick in only if the file does not exist on serverA.

Not exactly. I do want it to redirect everything to www. It was just only doing it if the URL referenced a file that existed on the non-www server (regardless of whether it was valid on www or not).

Nevertheless, this was clearly an issue outside the scope of my .htaccess file. I submitted a ticket to the hosting company and they have fixed the problem by adding an appropriate entry to "vhost conf". What exactly they added, I don't know.

Thanks very much for your help. At the very least I was able to verify that the lines I put into the .htaccess file were not incorrect.