多瑙河之野鸭's profile多瑙河之野鸭BlogLists Tools Help

Blog


    6/8/2008

    Google App Engine 添加 Favicon 和 robots.txt 的方法

    野鸭近日迷上了Google App Engine ,现在介绍 Google App Engine 使用中的两个小技巧。

    1. 添加 Favicon1 的方法

    一般情况下,Google App Engine 上传的是应用程序文件(如:Python 的 .py 文件),如果你要上传静态文件(如图片、Javascript、css),就得把它们放到一个静态目录里,并在 app.yaml 配置文件(详细的解说2)里做类似下面的定义:
    handlers:
    - url: /images
      static_dir: images
    - url: /css
      static_dir: css

    Favicon.ico 文件同样可以放到 images 目录下,上传后就可以通过地址 http://donau.appspot.com/images/favicon.ico 访问了。

    然后在 HTML 文件的 Head 里头添加 <link href="/images/favicon.ico" rel="shortcut icon" type="image/x-icon"> 就可以让别人浏览你的网站时,看到你的 Favicon 了。(考虑到兼容问题,我没有用 type="image/vnd.microsoft.icon" )

    但是,人家 favicon 默认都是放在网站的根目录下的,在 Google App Engine 里怎么实现呢?

    方法是在 app.yaml 配置文件内添加如下语句:(紧跟 handlers: 下面就好了)
    - url: /favicon.ico
      static_files: images/favicon.ico
      upload: images/favicon.ico
      mime_type: image/x-icon

    意思就是,当别人访问 http://donau.appspot.com/favicon.ico 时,我就用 images/favicon.ico 来代替输出。又因为 Google App Engine 不能根据 .ico 的后缀正确判断这个图标文件的 mime 类型,所以在这里明确指定了它的 mime_type 是 image/x-icon 。一方面,可以避免上传应用程序时出现“错误”提示;另一方面,可以直接在浏览器中打开这个文件,而不至于提示下载这个文件。(原来我是用  mime_type: image/vnd.microsoft.icon 的,但发现在 IE 下面,直接打开 icon 的地址,图标不能正常显示,所以还是用 x-icon 算了。)

    当把 favicon.ico 文件“放”到根目录之后,那句 <link href="/images/favicon.ico" rel="shortcut icon" type="image/x-icon"> 就可以省略不用了。因为现在的浏览器都知道默认从网站根目录找 favicon.ico 文件。

    的这个 http://donau.appspot.com/favicon.ico 是自己一点一点画的,咔咔。

    2. 添加 robots.txt3 的方法

    有了上面的经验,添加 robots.txt 就易如反掌了。我们同样可以把 robots.txt 放在 images 目录下,跟其它图片一起上传。只要在 app.yaml 配置文件中加上下面几句就好了:
    - url: /robots.txt
      static_files: images/robots.txt
      upload: images/robots.txt

    之前,竟然用了一个很土的方法。就是当别人访问 robots.txt 时,我专门用一个 .py 程序输出 robots.txt 里面的内容,真是太傻了。

    OK. 祝大家玩 Google App Engine 玩得开心!有空就来我家坐坐:http://donau.appspot.com/

    啊,差点忘了,今天是端午节(农历五月初五),祝大家节日快乐!吃粽子、划龙舟……

    © 2008 多瑙河之野鸭

    参考:
    1. http://en.wikipedia.org/wiki/Favicon
    2. Configuring an App - Google App Engine
    3. 如何创建 robots.txt 文件?

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://donauya.spaces.live.com/blog/cns!F53B4DB2F7D1A8A1!127.trak
    Weblogs that reference this entry
    • None