Completely fake URL with .htaccess

2019-09-08 15:43发布

I am serving static content with Amazon CloudFront and am using my servers as the origin.

Since CF does not respect ? query strings, I can't easily force CF to use new versions of .png files. I also need fast invalidation and do not want to pay for invalidation requests.

So I want to create a completely fake directory with .htaccess to force versioning for my images.

For instance, I want:

domain.com/static/0.12/images/background.png

going to

domain.com/static/images/background.png

Where 0.12 is my APP version, which will be automatically changed through my deployment script.

What would be the .htaccess rule for this?

2条回答
成全新的幸福
2楼-- · 2019-09-08 16:09

To force all requests from a URL like

example.com/static/images/background.png

to rewrite to internal URLs like

example.com/static/0.12/images/background.png

Use:

RewriteEngine On
RewriteRule ^static/images/(.*)$ static/0.12/images/$1 [L]
查看更多
劫难
3楼-- · 2019-09-08 16:18

If you actually want the URL

domain.com/static/0.12/images/background.png

to be internally redirected to

domain.com/static/images/background.png

Add the following to the .htaccess file in the root of your site.

RewriteEngine On
RewriteBase /

RewriteRule ^static/0\.12/images/(.+)$ static/images/$1 [L,NC]

If the APP version varies then use

RewriteEngine On
RewriteBase /

RewriteRule ^static/[\.0-9]+/images/(.+)$ static/images/$1 [L,NC]
查看更多
登录 后发表回答