I would like to add the following rewrite rule to htaccess, but I'm having trouble understanding exactly how to do it under wordpress.

RewriteEngine On
RewriteRule ^(data|cache)($|/) - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ redir.php?redir=$1 [L,QSA]

My goal is to have any directory under root that doesn't currently exist to redirect to my file redir.php and pass the name of the nonexistent directory as a variable. The above code works, but existing directories are also being passed to the redir.php file as well.

I tried taking a look at the Wordpress Codex for rewrites but everything I've tried adding to functions.php seems to yield no new results, or instead of being sent to redir.php I get a wordpress 404 page.

Any ideas?? Thank you in advance!

  • I'm not sure what this has to do with WordPress other than that a WP install is on the same server by coincidence? This looks like a general Apache HTAccess issue – Tom J Nowell Feb 12 '18 at 22:43
  • Because htaccess is handled by Wordpress differently than just any other site. – zoltar Feb 12 '18 at 22:46
  • 1
    It's wrong to say, "htaccess is handled by Wordpress differently", but at the same time this is not a "general Apache HTAccess issue" either. WP makes use of a front-controller pattern (as many sites do). It is this "front-controller" that is your "problem" here. As I answered on StackOverflow you can't do something like this in .htaccess, because of the front-controller, not strictly WP. You will need to solve this in WP, not .htaccess. And how you do this is specific to WordPress. – MrWhite Feb 13 '18 at 1:26
  • 1
    @zoltar WP knowledge on its own couldn't have answered that, you needed Apache knowledge to know about front controllers. Also keep in mind that in WP rewrite rules are a very different thing, the rewrite rule API maps URLs on to query variables for WP_Query, it can't be used to redirect execution on to another file such as redir.php. Without knowing what the context/purpose of redir.php is it's difficult to recommend something, eitherway you asked an Apache .htaccess question and got an answer elsewhere – Tom J Nowell Feb 13 '18 at 13:18
  • 1
    I recommend you mark the answer MrWhite wrote as correct, what you want to do can't be done due to Apache limitations. Then, ask a new question that explains your original problem that redir.php tries to solve, looking for a WP based solution. It may well be that the /data/ and /cache URLs being used are completely unnecessary, but without surrounding context that's difficult to determine – Tom J Nowell Feb 13 '18 at 13:20

Browse other questions tagged or ask your own question.