Change htaccess to redirect to index.php in subfolder

How can I change the WordPress root folder .htaccess file to redirect URLs with a specific subfolder to its own index.php and give the path data as a URL variable to the index.php?

https://example.com/subfolder/img.png

redirect to:

https://example.com/subfolder/index.php?path=img.png

all other redirects should work like WordPress expects them to.

Topic mod-rewrite url-rewriting htaccess Wordpress

Category Web


Try something like the following at the top of the root .htaccess file, before the existing WordPress directives (before the # BEGIN WordPress comment marker):

RewriteCond $2 !^(index\.php)?$
RewriteRule ^(subfolder)/(.+) /$1/index.php?path=$2 [L]

The RewriteCond directive is to ensure that it doesn't try to rewrite requests for index.php itself (or the directory). A request for /subfolder/ only will be served by /subfolder/index.php without the path URL parameter (by mod_dir).

Otherwise, all other requests to that subfolder (whether they map to files, directories or nothing at all) are passed to /subfolder/index.php in the query string.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.