Forum Moderators: phranque

Message Too Old, No Replies

Point any URL to specific file using mod rewrite

         

kevintynfron

2:01 pm on Aug 7, 2010 (gmt 0)

10+ Year Member



Hi there,

I'm taking a website offline to use locally in a field with no internet access for a few days. While I'm in the field, I want any URL that a user enters to point to a single file displaying a message informing them that they must turn up in person or call to change their details on the website.

I have tried variations of .htaccess in the root directory based around:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule http://localhost/offline.php


But I get errors or no redirection.

To be clear, I want

http://www.example.com/
http://www.example.com/dir1/
http://www.example.com/dir/?parameter=value
etc.


all to end up at

http://www.example.com/offline.php


Any ideas? I have access to httpd.conf if needed.

Thanks,
Kevin

g1smd

2:17 pm on Aug 7, 2010 (gmt 0)

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



A RewriteRule needs a pattern to match the URL request, a target filepath or URL, and both the R and L flags. You have missed two of those from your code.

However, doing it this way will seriously mess with your indexing and ranking, and might take months to recover.

You are better off serving a HTTP 503 response along with the explanatory message.

kevintynfron

3:28 pm on Aug 7, 2010 (gmt 0)

10+ Year Member



Thatnks for the reply.

OK, so I've now got this

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)$ http://localhost/offline.php [R,L]


So I'm looking for a URL that matches anything (start of line, any character repeated any number of times, end of line.
I'm replacing it with [localhost...] and it's redirecting and it's the last rule.

It kind of works with the URL changing, but firefox is saying
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.


I'm not worried about the SEO implications of doing this - The site is a private maintenance site that's password protected, so isn't on search engines anyway.

Cheers,
Kevin

kevintynfron

4:57 pm on Aug 7, 2010 (gmt 0)

10+ Year Member



Right, this:

RewriteEngine On
RewriteRule ^(.*)$ offline.php [L]


does the job OK, but doesn't change the URL in the browser.

Out of interest, how would I do that?

Many thanks,
Kevin

g1smd

10:37 pm on Aug 7, 2010 (gmt 0)

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



The rule with [R,L] was working in that it redirected the user to the specified URL.

The problem is hinted at in your Firefox error message, and is easy to understand if you use the Live HTTP Headers extension to see what your browser requests and what is in the response coming back from the server.

You'll see that after the first redirect to the specified URL, that the new URL still matches the rewriterule pattern and is therefore redirected again. Next time round it matches again and is redirected again, forever in an infinite loop.

The way to fix this is simply to use a preceding RewriteCond looking at REQUEST_URI using a ! negative match operator and testing for /offline.php appearing as the requested URL.

jdMorgan

11:42 pm on Aug 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or do the negative match in the rule itself:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule !^offline\.php$ http://localhost/offline.php [R=302,L]

However, this redirects *anything* except /offline.php to /offline.php. If offline.php includes any images, stylesheets, or external JavaScript code, then the requests for those will be redirected to /offline.php as well.

The solution for that is to use the RewriteCond approach, and exclude the URL-paths and/or filetypes that should not be redirected.

Basically, you need to re-consider your original concept of "redirect anything," because, as g1smd points out, that is where these problems come from.

Jim