简体   繁体   中英

Rewrite url for subdirectory in .htaccess

I moved wordpress files from root to a subdirectory. There are some files uploaded in wp-content/uploads folder. When accessing those files directly, I get a 404 error.

I need to rewrite the url only for the wp-content/uploads through .htaccess

What I need is to redirect from http://example.com/wp-content/uploads/2012/09/report.pdf to http://example.com/wp_sub/wp-content/uploads/2012/09/report.pdf

This is what I've tried

RewriteCond %{HTTP_HOST} ^(www.)?example.com$

RewriteRule ^(.*)$ http://www.example.com/wp_sub/ $1 [R=301,QSA,L]

This adds a subdirectory wp_sub to every link. I need to add a subdirectory "wp_sub" only for wp-content/uploads links.

What am I missing?

Let's see what

RewriteRule ^(.*)$ http://www.example.com/wp_sub/$1

means:
You rewrite everything ( ^(.*)$ ) to http://www.example.com/wp_sub/$1 .
Well, you don't want to rewrite everything, do you? :)

So we have to restrict this to only affect the wp-content folder:

RewriteRule ^(wp-content/.*)$ http://www.example.com/wp_sub/$1 [R=301,QSA,L]

Place this rule in your /wp-content/uploads/.htaccess file:

RewriteEngine On
RewriteBase /wp-content/uploads/

RewriteRule ^(.*)$ /wp_sub/$1 [L,R]

您可以使用以下代码:

   RewriteRule ^wp-content/uploads/(.*)$ http://www.example.com/wp_sub/wp-content/uploads/$1 [R=301,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM