Completely fake URL with .htaccess

Deadly 提交于 2019-12-12 02:54:53

问题


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?


回答1:


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]



回答2:


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]


来源:https://stackoverflow.com/questions/9003486/completely-fake-url-with-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!