1

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

MrWhite
  • 43,179
  • 8
  • 60
  • 84
Ans1827
  • 31
  • 1
  • If you need an introduction to how URL rewriting generally works, check [Reference: mod_rewrite, URL rewriting and "pretty links" explained](https://stackoverflow.com/q/20563772/1427878). – CBroe Feb 21 '23 at 07:48
  • Does this answer your question? [htaccess redirect from subfolder to new domain](https://stackoverflow.com/questions/28442682/htaccess-redirect-from-subfolder-to-new-domain) – Mohammad Salehi Feb 21 '23 at 08:22

2 Answers2

0

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.

Mohammad Salehi
  • 565
  • 1
  • 12
  • 33
  • 2
    "it redirects `https://www.example.com/wordpress/` to `https://www.example.com/`" - it would only do that if the `.htaccess` file was in the `/wordpress` subdirectory. (And if it is in the `/wordpress` subdirectory then the _condition_ that checks for "wordpress" in the URL is redundant.) The answer you link to is really solving a different problem. – MrWhite Feb 21 '23 at 23:43
  • Can you create a directory named `wordpress` and just put the `.htaccess` file in it? – Mohammad Salehi Feb 22 '23 at 13:09
0

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.

MrWhite
  • 43,179
  • 8
  • 60
  • 84