友好的URL的图像 [英] Friendly URL for images

查看:135
本文介绍了友好的URL的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请建议我该怎么投入的.htaccess 文件。

我有PNG,GIF,服务器上的JPG图片的http://domain.tld/images/anyimage.anyextension

想使网址更友好像的http://domain.tld/anyimage.anyextension


这个我现在有。如前两个字符串更改链接。但去年字符串不改回服务器。

 的RewriteCond%{REQUEST_URI} ^ /图像/(+)(\ GIF | \ JPG格式| \巴纽)$ [NC]
重写规则^图像/(.+)$ / $ 1 [R = 301,L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^ /(。+)$ /图片/ $ 1 [L]
 


如果我想补充

 的RewriteCond%{REQUEST_URI} ^ /([^ /] + \(PNG |。GIF | JPG格式))$ [NC]
的RewriteCond%{DOCUMENT_ROOT} /图像/%1 -f
重写规则^([^ /] + \(PNG |。GIF | JPG格式))$ /图像/ $ 1 [L,NC]
 

在右previous查询字符串规则,然后图像不开。如果在此之前没有任何问题。可能是什么?你有一个想法,如何解决呢?最后一个字符串重写规则^ /?(。+)$ /?$ 1 [L] 的原因这种冲突

 的RewriteCond%{THE_REQUEST} ^ [AZ] {3,9} \ / \?([^ \] +)[NC]
重写规则^ $ /%1? [R = 301,L]
的RewriteCond%{QUERY_STRING} ^ $
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^ /?(。+)$ /?$ 1 [L]
 

解决方案

不知道如何使它更加友好,除了它是较短的。您可以尝试在你的文档根目录下添加这对htaccess文件:

  RewriteEngine叙述上
的RewriteCond%{REQUEST_URI} ^ /([^ /] + \(PNG |。GIF | JPG格式))$ [NC]
的RewriteCond%{DOCUMENT_ROOT} /图片/%1 -f
重写规则^([^ /] + \(PNG |。GIF | JPG格式))$ /图片/ $ 1 [L,NC]
 

然后你可以从 HTTP更改所有链接://domain.tld/images/anyimage.anyextension 的http://域名.TLD / anyimage.anyextension

第一个条件检查,以确保请求的是 anyimage.anyextension ,只要扩展名是不区分大小写的PNG,GIF或JPG。

第二个条件检查,以确保所要求的图像确实存在于 /图片/ 目录。


  RewriteEngine叙述上
的RewriteCond%{THE_REQUEST} ^ [AZ] {3,9} \ /images/(.+)(\.gif|\.jpg|\.png)[NC]
重写规则^图像/(.+)$ / $ 1 [R = 301,L]
 

这将浏览器重定向到非/图像/ URL。有2怎么回事完全不同的事情。一个涉及的浏览器和其他处理服务器上的内容。请参见这样的回答,说明他们是如何不同。

这是你不工作的规则。第一:

 重写规则^ /(。+)$ /图片/ $ 1 [L]
 

永远不会匹配。 URI的用于匹配htaccess的文件重写规则的正则表达式都领先斜线删除,因此没有URI就要开始了 / 。你需要摆脱它。

其次,一旦你做,你会得到一个500内部服务器错误,因为你的规则会导致内部无限循环。你需要匹配%{THE_REQUEST} 来确保浏览器实际上是要求用一个网址 /图片/ 路径呢,不是已经在内部改写。

Please suggest me what to put into .htaccess file.

I have PNG, GIF, JPG images on server http://domain.tld/images/anyimage.anyextension

Want to make URLs more friendly like a http://domain.tld/anyimage.anyextension


This I have now. First two strings change links as described. But last string doesn't change it back for server.

RewriteCond %{REQUEST_URI} ^/images/(.+)(\.gif|\.jpg|\.png)$ [NC]
RewriteRule ^image/(.+)$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.+)$ /images/$1 [L]


If I add

RewriteCond %{REQUEST_URI} ^/([^/.]+\.(png|gif|jpg))$ [NC]
RewriteCond %{DOCUMENT_ROOT}/image/%1 -f
RewriteRule ^([^/.]+\.(png|gif|jpg))$ /image/$1 [L,NC]

Right after previous query string rule then images don't open. If before there's no problem. What it could be? Do you have an idea how to fix it? The last string RewriteRule ^/?(.+)$ /?$1 [L] cause this conflict

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?([^\ ]+) [NC]
RewriteRule ^$ /%1? [R=301,L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)$ /?$1 [L]

解决方案

Not sure how that makes it more friendly, aside from it being shorter. You can try adding this to the htaccess file in your document root:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([^/.]+\.(png|gif|jpg))$ [NC]
RewriteCond %{DOCUMENT_ROOT}/images/%1 -f
RewriteRule ^([^/.]+\.(png|gif|jpg))$ /images/$1 [L,NC]

Then you can change all of your links from http://domain.tld/images/anyimage.anyextension to http://domain.tld/anyimage.anyextension

The first condition checks to make sure the request is for anyimage.anyextension, as long as the extension is a case-insensitive png, gif, or jpg.

The second condition checks to make sure the requested image actually exists in the /images/ directory.


RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /images/(.+)(\.gif|\.jpg|\.png) [NC]
RewriteRule ^image/(.+)$ /$1 [R=301,L]

This redirects the browser to the non-/image/ URL. There's 2 completely different things going on here. One deals with the browser and the other deals with content on the server. See the top part of this answer that explains how they are different.

The rules that you have won't work. First:

RewriteRule ^/(.+)$ /images/$1 [L]

will never match. URI's used to match against the regex of a RewriteRule in htaccess files have the leading slash removed, so no URI is going to start with a /. You need to get rid of it.

Secondly, once you do, you'll get a 500 internal server error because your rules will cause an internal infinite loop. You need to match against %{THE_REQUEST} to ensure that a browser is actually requesting a URL with the /images/ path in it, not what has been internally rewritten.

这篇关于友好的URL的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆