skip to Main Content

I recently migrate WordPress website, but the old media URLs are being used by external apps and it’s kinda a hassle to change the external apps URLs

Old URLs:

https://www.example.com/wordpress/wp-content/...

New URLs:

https://www.example.com/wp-content/...

The old website was in wordpress subdirectory and the new one isn’t.

I want to redirect every URLS that’s www.example.com/wordpress/wp-content... to www.example.com/wp-content...

So far I used a plugin to redirect each media that I known of but I wonder if there’s a way to just do all

2

Answers


  1. This should work

    RewriteEngine ON
    RewriteBase /wordpress/
    RewriteCond %{HTTP_HOST} example.com
    RewriteCond %{REQUEST_URI} wordpress
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
    

    it redirects https://www.example.com/wordpress/ to https://www.example.com/ and keep path after /wordpress/

    Original answer: https://stackoverflow.com/a/28442973, I just tested this for OP situation.

    Login or Signup to reply.
  2. I want to redirect every URLS that’s www.example.com/wordpress/wp-content... to www.example.com/wp-content...

    If you want to keep the old media URLs (for these external apps) then you should internally rewrite the request, not externally "redirect" it. Redirecting will be slow (if the app actually follows the redirect) and potentially doubles the requests to your server.

    The following would need to go near the top of the root .htaccess file, before the existing WordPress code block.

    # /.htaccess
    
    # Rewrite old media URLs to new location
    RewriteRule ^wordpress/(wp-content/.+.(jpg|webp|gif))$ $1 [L]
    

    The $1 backreference refers to the first captured group in the preceding RewriteRule pattern. In other words, the part of the URL-path from wp-content/ onwards, including the filename. Ideally, you should be specific and match only the file-extensions you are interested in.


    Alternatively, keep the /wordpress subdirectory and in the /wordpress/.htaccess file use the following instead:

    # /wordpress/.htaccess
    
    RewriteEngine On
    
    # Rewrite old media URLs to new location
    RewriteRule ^(wp-content/.+.(jpg|webp|gif))$ /$1 [L]
    

    Note the absence of the wordpress subdirectory in the regex and the additional slash prefix on the substitution string (to rewrite to the root-directory).

    In using the /wordpress/.htaccess file it will completely override the mod_rewrite directives in the parent .htaccess file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search