Follow @BPSPro

Htaccess rule to redirect a specific URL to a custom error page (404 error page)

Comments Off RSS Site Feed Author: AITpro Admin
Published: June 26, 2010
Updated: September 29, 2010

The original question was regarding redirecting specific URLs in an .htaccess file to a custom 404 error page.  The answer contains specific and general information about the correct use of RewriteCond %{REQUEST_FILENAME}.

This is the correct usage of RewriteCond %{REQUEST_FILENAME}, but be careful this could cause very significant problems for you website if used incorrectly.  Please read the entire post before using this method of redirection in your .htaccess file.

The question was specific to redirecting any request for /comment/reply/###, no matter what ### is, to a specific page (404 error page).

This requires creating a separate .htaccess Mod in addition to your other .htacess mods

# Mod for redirecting specific URL requests to a custom error page
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} ^/comment/reply/[0-9]*$ /comment-error/ [NC]
RewriteRule . /your-error-page.php [L]
# End of custom mod

This info in this paragraph was for the particular overall problem this particular person had and does not come into play when creating a custom mod to redirect specific URLs. > Check and modify (if necessary) your WordPress Permalink structure under Settings > Permalinks. You should use a “Pretty Permalinks” custom structure of /%post_id%/%category%/%postname%/ or something similar. When you click Save Settings it will overwrite your existing .htaccess file so have a backup of your .htaccess file. Then to rule out a problem with your WordPress website links themselves install the WordPress plugin “Broken Link Checker”.

I think the approach you are taking is asking for problems so instead of taking the approach of redirecting a particular file request why not just block referrers? Seems more logical and less problematic to me, unless of course you have a particular unique reason for wanting to take this approach. If that is the case then add some more details and I will provide that answer, otherwise I think this approach is not the best way to take. FYI rewriterule follows conditions and it is best to have one rewriterule per set of conditions…and the rewriterule you added looks more like a condition to me. 😉

To Block multiple referrers in your .htaccess file you would add HTTP_REFFERER rewrite conditions. you can add as many as you want just remember to use the [NC,OR] flags on all of them except the last condition of course.

RewriteEngine on
RewriteCond %{HTTP_REFERER} badsite\.com [NC,OR]
RewriteCond %{HTTP_REFERER} anotherbadsite\.com [NC]
RewriteRule .* - [F]

.htaccess code FYI”s The NC flag is for not case sensitive so whether or not caps are used they will be blocked.
The R flag is for redirect.
The L flag is for last.
The F flag is for forbidden = Http 403 – is that a “special” enough error page? 😉
OR is for – OR next condition – you use OR on all rewritecond EXCEPT the last condition because there are no more conditions or “ors” 😉
lower case -f file lower case.
-d for directory the caret ^ stands for “is” exclamation mark in front of caret !^ stands for “is not”

By the way I believe mod_rewrite.c has been known to cause problems on certain hosts so it can or cannot be used if it works on your host. you don’t need it so i would just remove the entire ifModule directive.

What is does (info below is from the Apache website):

The … section is used to mark directives that are conditional on the presence of a specific module. The directives within an section are only processed if the test is true. If test is false, everything between the start and end markers is ignored.

The test in the section directive can be one of two forms:

module name = is module name
!module name = is not module name (exclamation mark)

In the former case, the directives between the start and end markers are only processed if the module named module name is included in Apache — either compiled in or dynamically loaded using LoadModule. The second format reverses the test, and only processes the directives if module name is not included.

The module name argument is the file name of the module, at the time it was compiled. For example, mod_rewrite.c. If a module consists of several source files, use the name of the file containing the string STANDARD20_MODULE_STUFF.

sections are nest-able, which can be used to implement simple multiple-module tests.

This section should only be used if you need to have one configuration file that works whether or not a specific module is available. In normal operation, directives need not be placed in sections.

Tags: ,

Categories: Wordpress Tips - Tricks - Fixes

Skip to toolbar