Refer to subfolder in .htaccess

2019-09-17 03:01发布

I'm trying to use Stacey CMS in conjunction with retina.js for my small portfolio site. Stacey tells all images inside a project folder to be added to the page. At the same time I'd like to serve @2x hi-dpi images. If I include these in the same folder, Stacey will add both the regular image.png and image@2x.png to the page, which I want to avoid.

To solve this, I want to rewrite all images ending with @2x to have the root /retina inside the same project folder. This folder is dynamic, ie. there are many different project folders, so I would like to have one rewrite rule to work for all.

I've gotten to this point with some help from a fellow stack overflow user:

RewriteRule ^(.*)@2x(.*)$ /Retina/$1@2x$2 [L]

This however, does not refer to a subfolder of the original project folder. How do I go about referring to the correct folder?

Edit: Alternatively there may be other ways to solve this issue? Changing retina.js retina image path

标签: .htaccess
1条回答
Bombasti
2楼-- · 2019-09-17 03:48

I think that the problem you encounter is do to an endless redirect loop. as your rule

RewriteRule ^(.*)@2x(.*)$ /Retina/$1@2x$2 [L]

Will try to redirect the call for the retina image uri like /image@2x.jpg but also again and again for the new url's /retina/image@2x.jpg as your rule only checks if the requested uri has the @2x string in it.

Try adding another condition to the rule that will prevent the rule to be processed for images that are in the retina folder

RewriteCond %{REQUEST_URI} !Retina
RewriteRule ^(.*)@2x(.*)$ /Retina/$1@2x$2 [L]

If the Retina folder is actually inside another folder and you have multiple Retina folders, meaning that when you call site.com/project1/image.png the retina version is in site.com/project1/retina/image@2x.png then you should try this:

RewriteCond %{REQUEST_URI} !Retina
RewriteRule ^(.*)/(.*)@2x(.*)$ /$1/Retina/$2@2x$3 [L]

in the Retina folders are inside the sub-folders, the first rewrite rule we used will have both the folder name & the image name in the variable $1, so what we should do is just break the folder name & the image name into two separate variables

查看更多
登录 后发表回答