Yii-Framework: Need Help On Url Rewriting

Discussion in 'PHP' started by FutureKing, Mar 7, 2013.

  1. #1
    Hi,
    I have developed an entire website in Yii-Framework. I am using Yii Url Rewrite.
    http://www.example.com/popular
    http://www.example.com/signup
    http://www.example.com/wallpaper/108/sexy-babes-1920x1200
    etc.

    My client asked that he wants url look like
    http://www.example.com/popular/
    http://www.example.com/signup/
    http://www.example.com/wallpaper/108/sexy-babes-1920x1200/

    (added slash in the end)

    Also he wants that if someone opens
    http://www.example.com/popular he/she should redirected to http://www.example.com/popular/

    http://www.example.com/wallpaper/108/sexy-babes-1920x1200 he/she should redirected to
    http://www.example.com/wallpaper/108/sexy-babes-1920x1200/

    He also wants to add this tag on every page of site:
    <link rel="canonical" href="http://www.example.com/popular/" />

    How to do this?
    Please help. I have to deliver this project ASAP.
     
    FutureKing, Mar 7, 2013 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    What does your .htaccess file look like now?
     
    MyVodaFone, Mar 8, 2013 IP
  3. FutureKing

    FutureKing Well-Known Member

    Messages:
    194
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    125
    Digital Goods:
    4
    #3
    Hi,

    I ended up with this solution:

    I placed this code in global controller class constructor(i.e. the class from which all controller classes extend)

    $slash = substr($this->request->url, -1);
    $url = $this->request->hostInfo.$this->request->url;
    if($slash != "/"){
    $this->redirect($this->request->url."/",true,301);
    $url = $this->request->hostInfo.$this->request->url."/";
    }
    Yii::app()->getClientScript()->registerLinkTag("canonical",null,$url);
     
    FutureKing, Mar 8, 2013 IP