Forum Moderators: phranque

Message Too Old, No Replies

internal redirect all requests to one file

         

jboy

8:10 pm on Dec 5, 2010 (gmt 0)

10+ Year Member



Hello, I'm trying to redirect all requests to one particular file. I have this in my .htaccess file:


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ /for-all-requests.php [NC,L]


It works when the file being requested doesn't exist (works as in, the for-all-requests.php is accessed). But when the file being requested exists, the response is the file stated in the user's request, not the for-all-requests.php file. How to make *all* requests go to /for-all-requests.php ?

Thanks.

jboy

8:14 pm on Dec 5, 2010 (gmt 0)

10+ Year Member



Oh yeah, I forgot, I guess (to a certain extent know) this:
RewriteCond %{REQUEST_FILENAME} !-f
says "if the file doesn't exist" -- which in my case is exactly what I don't want. But without that there's an internal server error. I guess there's something which needs to go in the place of that line, but what?

Thanks.

g1smd

8:38 pm on Dec 5, 2010 (gmt 0)

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



Does this file then send a 404 HTTP header as the very first thing that it does?

If not, your website will have infinite URL space, and when searchengine spiders discover that fact, they'll flag it as a spider trap and limit their attempts at indexing it.

As you likely do not want requests for images, scripts, or stylesheets, etc, to resolve to this file then a negative match RewriteCond testing for those requests is probably something that is useful to add.

I cannot stress enough that your site must return the correct HTTP status code for all requests (whether intended or unintended) arriving at the server.

jboy

8:43 pm on Dec 5, 2010 (gmt 0)

10+ Year Member



I've found someone else having exactly the same problem, but his solution doesn't work:

[forum.kohanaframework.org...]

Also he's talking about an infinite loop; I don't think that's what I'm getting. I get an internal server error immediately, but maybe an infinite loop is causing that?

So what I tried in light of that linked to post is:


RewriteEngine on
RewriteCond %{REQUEST_URI} !^(/for-all-requests.php)
RewriteRule ^.*$ /for-all-requests.php [NC,L]

g1smd

8:49 pm on Dec 5, 2010 (gmt 0)

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



What is this file actually *for*?

What are you trying to achieve?

The usual way to serve an error message for content not found is to use something like:

ErrorDocument 404 /error404.php

jboy

9:08 pm on Dec 5, 2010 (gmt 0)

10+ Year Member



I've finally got it to work:


RewriteEngine on
RewriteCond %{REQUEST_URI} !^(.*for-all-requests\.php) [NC]
RewriteRule ^(.*)$ /for-all-requests.php [L]


Man alive. The good thing about this is, I can now use PHP rather than Apache to do both internal and external redirects. Phew.

jboy

9:10 pm on Dec 5, 2010 (gmt 0)

10+ Year Member



> What is this file actually *for*?

Ah, I posted that last message before I saw yours; but happenend to answer your question anyway. Thanks.

jboy

9:13 pm on Dec 5, 2010 (gmt 0)

10+ Year Member



Oh blimey, sorry, I've only just seen your first post in this thread. I'm sure that wasn't there when and after I posted my third message. Anyway, this file will do the external redirects I want to happen, then, if no external redirects necessary, it'll output the right file if it exists, and if it doesn't, then it'll output the 404 file. Thanks.