{"id":995,"date":"2010-06-26T09:29:07","date_gmt":"2010-06-26T16:29:07","guid":{"rendered":"http:\/\/www.ait-pro.com\/aitpro-blog\/?p=995"},"modified":"2010-09-29T23:50:07","modified_gmt":"2010-09-30T06:50:07","slug":"htaccess-rule-to-redirect-a-specific-url-to-a-custom-error-page-404-error-page","status":"publish","type":"post","link":"https:\/\/www.ait-pro.com\/aitpro-blog\/995\/misc-projects\/wordpress-tips-tricks-fixes\/htaccess-rule-to-redirect-a-specific-url-to-a-custom-error-page-404-error-page\/","title":{"rendered":"Htaccess rule to redirect a specific URL to a custom error page (404 error page)"},"content":{"rendered":"<p>The original question was regarding redirecting specific URLs in an .htaccess file to a custom 404 error page.\u00a0 The answer contains specific and general information about the correct use of RewriteCond %{REQUEST_FILENAME}.<\/p>\n<p>This is the correct usage of RewriteCond %{REQUEST_FILENAME}, but be careful this could cause very significant problems for you website if used incorrectly.\u00a0 Please read the entire post before using this method of redirection in your .htaccess file.<\/p>\n<p>The question was specific to\u00a0redirecting any request for \/comment\/reply\/###, no matter what ### is, to a specific page (404 error page).<\/p>\n<p>This requires creating a separate .htaccess Mod in addition to your other .htacess mods<\/p>\n<p><code># Mod for redirecting specific URL requests to a custom error page<br \/>\nRewriteEngine On<br \/>\nRewriteBase \/<br \/>\nRewriteCond %{REQUEST_FILENAME} ^\/comment\/reply\/[0-9]*$ \/comment-error\/ [NC]<br \/>\nRewriteRule . \/your-error-page.php [L]<br \/>\n# End of custom mod<\/code><\/p>\n<p>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 &#8220;Pretty Permalinks&#8221; 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 &#8220;Broken Link Checker&#8221;.<\/p>\n<p>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&#8230;and the rewriterule you added looks more like a condition to me. \ud83d\ude09<\/p>\n<p>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.<\/p>\n<p><code>RewriteEngine on<br \/>\nRewriteCond %{HTTP_REFERER} badsite\\.com [NC,OR]<br \/>\nRewriteCond %{HTTP_REFERER} anotherbadsite\\.com [NC]<br \/>\nRewriteRule .* - [F]<\/code><\/p>\n<p>.htaccess code FYI&#8221;s The NC flag is for not case sensitive so whether or not caps are used they will be blocked.<br \/>\nThe R flag is for redirect.<br \/>\nThe L flag is for last.<br \/>\nThe F flag is for forbidden = Http 403 &#8211; is that a &#8220;special&#8221; enough error page? \ud83d\ude09<br \/>\nOR is for\u00a0&#8211; OR next condition &#8211; you use OR on all rewritecond EXCEPT the last condition because there are no more conditions or &#8220;ors&#8221; \ud83d\ude09<br \/>\nlower case -f file lower case.<br \/>\n-d for directory the caret ^ stands for &#8220;is&#8221; exclamation mark in front of caret !^ stands for &#8220;is not&#8221;<\/p>\n<p>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&#8217;t need it so i would just remove the entire ifModule directive.<\/p>\n<p>What is does (info below is from the Apache website):<\/p>\n<p>The &#8230; 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.<\/p>\n<p>The test in the section directive can be one of two forms:<\/p>\n<p>module name = is module name<br \/>\n!module name = is not module name (exclamation mark)<\/p>\n<p>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 &#8212; 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.<\/p>\n<p>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.<\/p>\n<p>sections are nest-able, which can be used to implement simple multiple-module tests.<\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The original question was regarding redirecting specific URLs in an .htaccess file to a custom 404 error page.\u00a0 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.\u00a0 [&hellip;]<\/p>\n","protected":false},"author":167,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[13],"tags":[208,207],"class_list":["post-995","post","type-post","status-publish","format-standard","hentry","category-wordpress-tips-tricks-fixes","tag-htaccess-condition-to-redirect-urls","tag-htaccess-rule-to-redirect-a-specific-url"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.ait-pro.com\/aitpro-blog\/wp-json\/wp\/v2\/posts\/995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ait-pro.com\/aitpro-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ait-pro.com\/aitpro-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ait-pro.com\/aitpro-blog\/wp-json\/wp\/v2\/users\/167"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ait-pro.com\/aitpro-blog\/wp-json\/wp\/v2\/comments?post=995"}],"version-history":[{"count":0,"href":"https:\/\/www.ait-pro.com\/aitpro-blog\/wp-json\/wp\/v2\/posts\/995\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ait-pro.com\/aitpro-blog\/wp-json\/wp\/v2\/media?parent=995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ait-pro.com\/aitpro-blog\/wp-json\/wp\/v2\/categories?post=995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ait-pro.com\/aitpro-blog\/wp-json\/wp\/v2\/tags?post=995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}