url rewriting - Change htaccess to redirect to index.php in subfolder

admin2025-01-07  3

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?

.png

redirect to:

.php?path=img.png

all other redirects should work like WordPress expects them to.

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.

Share Improve this question edited Dec 8, 2021 at 0:36 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Dec 7, 2021 at 11:38 HHGKHHGK 393 bronze badges 1
  • I assume you mean "rewrite" as opposed to "redirect"? I assume the URL is not meant to change in the browser's address bar. – MrWhite Commented Dec 8, 2021 at 0:37
Add a comment  | 

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736263819a920.html

最新回复(0)