简体   繁体   中英

Rewrite rule from directory to subdirectory in same domain

I have the following URL:

http://example.com/media/image.png

This should be redirected to:

http://example.com/media/images/image.png

This rule is only to redirect images in media folder.

But the rule should NOT redirect this kind of URLs:

http://example.com/some_folder/media/image.png

To:

http://example.com/media/images/image.png

How it should be?

I tried things like this:

RewriteRule /media/(.*) /media/images/$1 [R,L]

But I didn't get good results.

You need to anchor your expression, otherwise it will match any URL containing /media/ as opposed to starting with media/

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

If it's only images, you might want to consider making the expression more strict, such as:

RewriteRule ^media/(.+\.(jpe?g|gif|png)) /media/images/$1 [R,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