Skip to contentSkip to main navigation Skip to footer

Redirect Images From The Old Paths to The New Paths

If you change the image paths using Hide My WP Ghost plugin, it is important not to have the same image on two different URLs on a website to avoid duplicate content issues and to maintain good search engine optimization (SEO) practices.

When search engines like Google detect duplicate content, it can negatively impact the website’s visibility and rankings in search results.

  1. Set a new uploads path. Choose a directory name in Hide My WP > Change Paths > WP Core Security like “storage” where you want to store the uploaded images.
  1. Add the below code in the config file.

For Apache/Litespeed, locate the .htaccess file in the root directory of your website and add the following code at the beginning of the file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} /wp-content/uploads/[^\.]+(\.jpg|\.png|\.jpeg|\.webp|\.gif) [NC]
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-content/uploads/(.*)$ /storage/$2  [L,R=301]
</IfModule>

For Nginx servers add this code before the Hide My WP Ghost include line in the Nginx config file:

  if ($request_uri ~* ^/wp-content/uploads/[^\.]+(\.jpg|\.png|\.jpeg|\.webp|\.gif)){
    rewrite ^/wp-content/uploads/(.*)$ /storage/$1 redirect;
  }

This code utilizes mod_rewrite, a module that allows URL rewriting. It checks if the request is for an image file within the /wp-content/uploads/ directory and redirects it to the corresponding file in the /storage/ directory.

By implementing this solution, any requests for images in the old /wp-content/uploads/ the directory will be redirected to the new /storage/ directory, preventing duplicate URLs for the same image.

Learn how to hide the old image paths from users:

https://hidemywpghost.com/faqs/how-can-i-hide-the-old-images-paths/