Home htaccess redirect any folder to file with parameters in wordpress
Reply: 1

htaccess redirect any folder to file with parameters in wordpress

zoltar
1#
zoltar Published in 2018-02-12 22:49:14Z

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
RewriteCond %{REQUEST_METHOD} !POST
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!

MrWhite
2#
MrWhite Reply to 2018-02-13 14:48:10Z

My goal is to have any directory under root that doesn't currently exist to redirect to my file redir.php ...

You can't do something like this in .htaccess with WordPress (or any site that uses a front-controller pattern) since there is no way to determine what is a "directory that does not exist" and a valid WordPress URL, which also does not exist on the filesystem.

If you implement something like this in .htaccess then you will essentially override the WordPress site (so none of your WordPress URLs will work).


To implement this with WordPress you will need to do it entirely within WordPress (in PHP). Only when you have determined the request does not map to a valid WordPress URL (ie. would otherwise result in a 404) then you could divert the request to your redir.php script. (But .htaccess executes to early in the request to be able to do that.)

Related question on the WordPress stack (closed as being off-topic, probably because it is focusing on the perceived solution (ie. .htaccess), rather than the actual problem: Getting WordPress to execute a custom function on a request that would otherwise result in a 404):
https://wordpress.stackexchange.com/questions/293931/trouble-adding-directory-rewrite-to-htaccess-under-wordpress

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.322711 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO