Question:
How do I redirect a set of URLs with regular expressions in .htaccess for a wordpress installation?
Douglas
2010-04-17 09:14:21 UTC
Just transfered my site from Drupal to WordPress. Generating a lot of 404's for URLs that don't exist, probably from spam bots. For example /comment/reply/123 or some other number.

I want to redirect any request for /comment/reply/###, no matter what ### is, to a specific page.

For example, domain.com/comment/reply/123 and domain.com/comment/reply/321 etc. should all go to domain.com/comment-error

I tried this:
RewriteRule ^/comment/reply/[0-9]*$ /comment-error/ [R]
... and variations on the above, in the existing .htaccess file as follows:


RewriteEngine On
RewriteBase /
RewriteRule ^/comment/reply/[0-9]*$ /comment-error/ [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


All but the line I added is a part of WordPress.

It didn't work. It didn't break the .htaccess file, but it ignored URLs I typed in following the pattern that it was supposed to redirect to /comment-error/

How do I redirect all URLs matching a specific pattern to a special error page?
Three answers:
2010-04-20 10:14:38 UTC
Check and modify (if necessary) your Wordpress Permalink structure under Settings > Permalinks. You should use a "Pretty Permalinks" custom structure of /%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

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



Hope that helps. Good luck!

PS I have an .htaccess file that you can dowload here >>> http://www.ait-pro.com/aitpro-blog/wordpress-tips-tricks-fixes/bulletproof-htaccess-file-code-wordpress-bulletproof-htaccess-code/ that blocks XSS and SQL Injection hacking attacks. I'm in the final phase of writing a new Wordpress plugin that will do some interesting stuff with it. ;)

UPDATE to your question - not advisable, but this is the answer. Don't blame me if you loose visitors. ;)

Create a separate mod

RewriteCond %{REQUEST_FILENAME} ^/comment/reply/[0-9]*$ /comment-error/ [NC]

RewriteRule . /your-error-page.php [L]
?
2016-06-02 06:54:09 UTC
Hi =) I know lately it's happened here a lot... what about other sections though? And do they come closer like a lot have here? Hmmm Oh well, Off w/the trolls heads O_O
21stsoft
2010-04-20 09:34:43 UTC
Looks like a similar problem was solved here:

http://www.webmasterworld.com/apache/4005167.htm



I am sure it will take some experimentation, and I'm curious as to what the final solution looks like, so please post it if you get there.



You might also send the question to your hosting company and see if they have solved this before for others.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...