Need Help On Url Rewriting

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.

Add this to url manager:


'urlSuffix' => '/',

I’m sure you can work out the canonical part.

A friendly tip…

Personally I hate it when people ask questions and then say it’s urgent. I do this in my free time for no pay and don’t give a damn how urgent somebody else’s project is. I consider it disrespectful imho.

Thanks. It is now generating urls with "/" in end but if someone requests a url without "/" in end then it should redirect to the "/" version. How to do this redirection?

Are you Google-phobic or something? :)

http://enarion.net/web/htaccess/trailing-slash/

I already checked that page. I don’t understand all the htaccess code perfectly. Therefore when I used the code from that url it also didn’t worked for me.

Here is my .htaccess file

Options +FollowSymLinks

IndexIgnore /

RewriteEngine on

if a directory or a file exists, use it directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

otherwise forward it to index.php

RewriteRule . index.php

Now please tell me what should I modify?

htaccess rules are tricky. So just try.

It’s a valuable skill to acquire.

As Jacmoe says…

Keep up your search on htaccess until you find something appropriate.

It’s not Yii specific and you might find it doesn’t work well on localhost with root in a sub-folder anyway.

It’s kind of a “suck and see” kind of thing specific to your situation.

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 != "/"){

 &#036;this-&gt;redirect(&#036;this-&gt;request-&gt;url.&quot;/&quot;,true,301);

$url = $this->request->hostInfo.$this->request->url."/";

}

Yii::app()->getClientScript()->registerLinkTag("canonical",null,$url);