Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  w3d Dec 20 '13 at 13:11
add comment

migrated from serverfault.com Feb 3 '11 at 2:38

This question came from our site for professional system and network administrators.

4 Answers

up vote 3 down vote accepted

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).

share|improve this answer
    
Thank you very much! –  Chris Laplante Mar 6 '11 at 1:53
add comment
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.

share|improve this answer
add comment

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. :)

share|improve this answer
add comment

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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