dprxj1995
dprxj1995
2011-01-30 20:53

php中的全局重定向从something.com到www.something.com

  • global
  • redirect
  • php
已采纳

I'm a .net developer and I've just started developing a site on PHP running on IIS7-fastcgi.

I was wondering if there is any way to write a global function to automatically redirect any request that comes to my site in the format

http://something.com/anypage.php 
to
http://www.something.com/anypage.php

You can do this in ASP.NET on the global.asax file. IS there any way to do this or set this condition in php?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • douge3830 douge3830 10年前

    I don't think so..but you can do this using .htaccess

    (edited to include comment below):

    or the iis7 equivalent I guess is what you want http://learn.iis.net/page.aspx/557/translate-htaccess-content-to-iis-webconfig/

    点赞 评论 复制链接分享
  • dongtangjie0495 dongtangjie0495 10年前
    if ($_SERVER['HTTP_HOST'] == "something.com") {
        header("Status: 301 Moved Permanently");
        header("Location: http://www.something.com" . $_SERVER['REQUEST_URI']);
        exit;
    }
    
    点赞 评论 复制链接分享
  • doujiao6116 doujiao6116 10年前

    Yes, you must have .htaccess in your root directory, that says :

    Redirect 301 http://something.com/anypage.php  http://www.something.com/anypage.php 
    

    But im not sure if IIS supports .htaccess files :)

    点赞 评论 复制链接分享
  • doushi7819 doushi7819 10年前

    You can do this in IIS by using the URL Rewrite module.

    点赞 评论 复制链接分享