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
This should work
it redirects
https://www.example.com/wordpress/
tohttps://www.example.com/
and keep path after/wordpress/
Original answer: https://stackoverflow.com/a/28442973, I just tested this for OP situation.
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.The
$1
backreference refers to the first captured group in the precedingRewriteRule
pattern. In other words, the part of the URL-path fromwp-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: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.