2

I have a number of rules governing the URLs on my main website & it's a pretty nice setup. There is an unintended consequence that has come up though. My latest addition cleans up a bunch of 500 errors and allows nonsense URLs to redirect a default page instead.

I need to direct some such URLs to 404 instead but I'm having trouble getting the rule to work. Here is the working rule as it stands right now. I'd like a version of this that directs the request to 404 instead given a specific page.php.

This version effects anything ending with .php/ which might be ok but it would be nice to see one configured for only a single, specific file. Also I'm thinking the redirect in the 1st rule should probably be 301 no?

## STRIP ANYTHING AFTER .php/ ##
RewriteCond %{THE_REQUEST} /([^.]+)\.php/? [NC] 
RewriteRule ^ /%1/ [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /$1.php [L,NC]
1
  • Much of whether your intent is possible depends heavily on the traffic. For example, bad bot traffic or hack attempts often do not follow redirects. This may be why you are not getting it to work. I do something similar, my example will not help you, and the result is that only about %25 (guessing here) actually do redirect.
    – closetnoc
    Feb 29, 2020 at 0:54

1 Answer 1

1

You just need to add any rules that target specific URLs before your more generalised redirect.

Also note that, generally, you should deliver a 404 with an internal subrequest, not an external "redirect" (as you describe). So, if you want to return a specific 404 message, you should also define a custom error document with the ErrorDocument directive.

For example:

ErrorDocument 404 /my-error-documents/error404.php

# Respond with a 404 for specific URL
RewriteRule ^page\.php$ - [R=404]

# Other rules follow...

Request /page.php and /my-error-documents/error404.php will be served via an internal subrequest. The URL remains as /page.php in the browser. A "404 No Found" is automatically sent by Apache. Despite the use of the R flag, this is not an external redirect (which only applies to 3xx codes).

Note that the substitution string is a single hyphen (indicating "no substitution"). There is no need to include the L flag when using a non-3xx status code.

Also I'm thinking the redirect in the 1st rule should probably be 301 no?

If this is intended to be permanent and you have tested that this works as intended then use a 301 code. ie. R=301


Aside:

RewriteRule ^ /%1/ [NC,R,L]

On a rule like this, where you're not actually matching anything, or where the pattern already encompases all upper and lowercase letters (as in your second rule) then the NC flag is entirely superfluous.

Your Answer

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

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