.htaccess Subfolder Redirect

2019-07-27 10:09发布

I recently moved my WordPress website to a subfolder. I want to add a redirect in the .htaccess file so that the links to images I've uploaded originally to ~/wp-content/uploads/ will pass to ~/blog/wp-content/uploads. The .htaccess file must remain in the root folder for WordPress to read it properly. This is what I tried

Redirect /wp-content/uploads/ /blog/wp-content/uploads

That worked great, except I host other domains on this account, and all of the other domain's upload folders were being redirected in a similar manner.

Is there a way to restrict this redirect to just one domain? So that example.com/wp-content/uploads redirects to example.com/blog/wp-content/uploads, but another.com/wp-content/uploads does not?

Thanks everyone!

2条回答
唯我独甜
2楼-- · 2019-07-27 10:27

Assuming you want a 301 redirect, using this RewriteEngine example should work:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteRule ^wp-content/uploads/(.*)$ http://www.example.com/blog/wp-content/uploads/$1 [R=301,L]
查看更多
在下西门庆
3楼-- · 2019-07-27 10:44

You should place the Redirect inside the <VirtualHost> definition for example.com in the httpd.conf (or equivalent) instead of .htaccess

on a sidenote, Redirectsays temporary / 302 by default, so it is nicer to use

Redirect permanent /wp-content/uploads/ /blog/wp-content/uploads instead

查看更多
登录 后发表回答