3

I'm setting up a mod_rewrite rule that redirects failing pages to a custom "Page Not Found" page. This is with WordPress. I've used an online tutorial from the Apache site and so far my rules look like this:

RewriteCond   %{REQUEST_FILENAME} !-f
RewriteRule   ^(.+)   http://example.com/?page_id=254 [R]

This works. It seems to be a combination of the first and second suggestion that worked, since the -U flag did nothing.

My question is, out of curiosity why the following happens: When I change REQUEST_FILENAME to REQUEST_URI (as the second example suggests), the page loads, but none of the style sheets load. All of my formatting is gone, and this happens on every page. Can anyone think of why this might happen?

1
  • By doing an external redirect for what should be a "404 File Not Found" you end up returning the wrong HTTP status code to the client. ie. a 302 (temporary Redirect), instead of a 404. The ErrorDocument is the correct approach as detailed in tadamson's answer.
    – MrWhite
    Commented Dec 20, 2013 at 13:11

4 Answers 4

4

For setting custom 404s, you should be able to do that much easier (even in .htaccess), like so:

ErrorDocument 404 http://example.com/?page_id=254

See also: ErrorDocument

Less messy and significantly less CPU use per request than mod_rewrite - anything that 404s just goes straight there, no questions asked. I'd definitely consider making any errordoc a static page if possible though, especially if you stretch it to handle other errors (no fun when your 500 errordoc throws a 500 error).

1
  • Note that if you specify an absolute URL (as above) for the error document, it will result in an external redirect when the error document is triggered - which is generally not desirable. You normally want to specify a root-relative path (starting with a slash) which will then rewrite the request internally.
    – MrWhite
    Commented Oct 31, 2015 at 19:03
0

Very strange. In FF you can use Firebug or Tamper Data for debug. Also see source code(Ctrl+U) and click to stylesheet link in it. Also see Error Console(Cntl+Shift+j).

0

If you're talking about WordPress, it may already be using redirects so that REQUEST_URI is not being set to what you expect--and is, thus, failing. For example, if its accessing ?page_id=100, then the request URI may, in fact, be an empty string. Even though it may be using index.php behind the scenes, at the time of your conditional, its likely not being set.

You can turn on mod_rewrite debugging to see the different steps the pattern matching makes, and I highly suggest you do so if you want to find out what's going on. :)

0
RewriteCond   %{REQUEST_FILENAME} !-f

My question is, out of curiosity why the following happens: When I change REQUEST_FILENAME to REQUEST_URI (as the second example suggests), the page loads, but none of the style sheets load. All of my formatting is gone, and this happens on every page.

REQUEST_FILENAME and REQUEST_URI contain different values in per-directory htaccess files. REQUEST_FILENAME is the full local filesystem path to the file, whereas REQUEST_URI is the path component of the requested URI (which is not necessarily an actual file on the filesystem).

-f tests whether the TestString is a regular file, which the REQUEST_URI is often not - this is probably why requests for your stylesheets are being redirected.

It would seem, however, that the REQUEST_URI for your main page is being seen as a regular file (or other directives are getting to the request first). This will depend on your URL structure and possibly other directives in your .htaccess file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.