Support

Admin Tools for WordPress

#41199 Can't get redirect from file within wp-upload to new url to work

Posted in ‘Admin Tools for WordPress’
This is a public ticket

Everybody will be able to see its contents. Do not include usernames, passwords or any other sensitive information.

Environment Information

WordPress version
6.6.2
PHP version
8.3
Admin Tools version
1.6.7

Latest post by nicholas on Monday, 14 October 2024 06:30 CDT

sumawelt

Hi,
I'm trying to redirect a pdf file to a new url:

Old url is: https://domain.tld/wp-content/uploads/2022/03/Terminanfrage.pdf and I want to redirect it to https://otherdomain.de.
Other redirects, I set up, are working fine. It seems to be related to the wp-content directory. When I try to redirect the pdf from anywhrere within the wp-content directory, I get a 404, If I redirect it from the sites' root, it works fine.

I tried it using the redirect feature in admin tools and via the settings in the .htaccess maker. Tried:

RewriteRule ^wp-content/uploads/2022/03/Terminanfrage.pdf$ https://otherdomain.de[R=301,L,NC]

neither in "Custom .htaccess rules at the top of the file" nor in "Custom .htaccess rules at the bottom of the file" the rewrite instructions were effective.

(tried to edit my previous ticket, but wasn't able to edit the text, so I closed it an created a new one, sorry for the mess....)

 

nicholas
Akeeba Staff
Manager

Off the top of my head, I believe that the expression should be

RewriteRule ^/wp-content/uploads/2022/03/Terminanfrage.pdf$ https://otherdomain.de[R=301,L,NC]

Note the forward slash after the caret. The ^ character in a POSIX RegEx (the kind of RegEx Apache uses) matches the starting position of the string. RewriteRule operates on the URL path which always includes the leading slash in Apache as per Apache's manual:

In per-directory context (Directory and .htaccess), the Pattern is matched against only a partial path, for example a request of "/app1/index.html" may result in comparison against "app1/index.html" or "index.html" depending on where the RewriteRule is defined.

The directory path where the rule is defined is stripped from the currently mapped filesystem path before comparison (up to and including a trailing slash). The net result of this per-directory prefix stripping is that rules in this context only match against the portion of the currently mapped filesystem path "below" where the rule is defined.

Directives such as DocumentRoot and Alias, or even the result of previous RewriteRule substitutions, determine the currently mapped filesystem path.

This is impossible to comprehend as it's written. Basically, your virtual host maps a filesystem path, let's say /home/myuser/www, to the domain name www.example.com. Therefore the URL https://www.example.com/foo/bar is internally represented as something like VirtualHostRoot/foo/bar. For RewriteRule purposes in a .htaccess file placed in the web root (i.e. what WordPress has), Apache throws away the VirtualHostRoot part and we have /foo/bar with a leading slash. 

Nicholas K. Dionysopoulos

Lead Developer and Director

🇬🇷Greek: native 🇬🇧English: excellent 🇫🇷French: basic • 🕐 My time zone is Europe / Athens
Please keep in mind my timezone and cultural differences when reading my replies. Thank you!