[WordPress] Replacing static files to make them redirect URLs.

I wanted to replace a PDF file uploaded to WordPress.

Specifically, replace file01.pdf with file02.pdf.
On the screen, all you have to do is change the href of the a tag.

However, this is not reflected in search results such as Google search.
For example, if there is ‘file01.pdf’ in the Google search results and you click on it, ‘file01.pdf’ will be displayed.
In this case, I wanted to 301 redirect it so that ‘file02.pdf’ would be displayed.

In this case, the server environment was managed by a different person in charge and it was not possible to change Apache, so the files had to be 301 redirected using only the Redirection plugin.

Translated with DeepL.com (free version)

目次

Redirect with the Redirection plugin.

What we want to achieve is that when the URL ‘example.com/wp-content/uploads/2024/11/file01.pdf’ in the Google search results is clicked, it will redirect to ‘example.com/wp-content/uploads/2024/12/ It is to redirect to ‘file02.pdf’.

In conclusion, if the redirect source file ‘example.com/wp-content/uploads/2024/11/file01.pdf’ is deleted from the media library and the Redirection plugin is used to set the redirect, the redirect will occur.

It is important to delete the redirect source file ‘example.com/wp-content/uploads/2024/11/file01.pdf’ and make it a non-existent URL.

Why can’t static files (.pdf, .jpg, .png etc) be redirected by the Redirection plugin?

This is due to the basic request handling of web servers, not just Apache.
Many web servers (Apache, Nginx, etc.) handle requests for static files (.pdf, .jpg, .png, etc.) directly in order to optimise performance.

This means that

  • Static files such as .pdf are returned intact by the server and do not pass through PHP processing.
  • Redirects in the Redirection plugin configuration have to go through PHP or Apache rules, but static file requests bypass this process.

The web server works as follows, so if the static file of the redirection source is deleted, the Redirection plugin did not work.

  1. Check if the actual file exists (‘/wp-content/uploads/2024/11/file01.pdf’).
  2. If the file exists, return the file directly (at this point it does not pass through PHP or .htaccess Rewrite rules).
  3. If the file does not exist, then pass the process to the .htaccess rules or PHP.

The Redirection plugin also mentioned that. Official documentation is great.

よかったらシェアしてね!
目次