Access files at new location using old file paths

I have a bunch of posts that contain images with urls that follow this pattern:

https://example.com/example_directory/files/2019/04/file.png

I’ve imported the posts into a new install of wordpress on the same domain with file structure where the files now reside at

https://example.com/new_directory/2019/04/file.png

Because “example_directory” is now a category slug, I cannot simply update the new directory structure to mirror the old one.

How can I make the files inside new_directory appear when my browser tries to visit urls at directory/files?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I believe I’ve found the solution. I add the following

RewriteRule ^example_directory/files/(.*) new_directory/$1

to my WordPress .htaccess rules like so:

RewriteEngine On
RewriteBase /
RewriteRule ^example_directory/files/(.*) new_directory/$1
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

I couldn’t get it working before and I think the key was moving the new RewriteRule directive to the line directly following RewriteBase /


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x