how to redirect and add # in the end

i want to redirect my page to this url:

i tried a lot of ways with $this->redirect they all replace the # with %23


As far as I know, you can’t server-side redirect to the hash part of a link - the hash part is only managed by the client (browser), so while you can generate a link including a hash, that will work if a user clicks on it in his browser (because the browser will load the relevant page then look for the anchor in it), server side you can only redirect to a page and not within that page. This is inherent in HTTP/HTML and nothing to do with Yii.

sorry but i think you did not understand me

i have a website in one of my page i have a div with id="example"

now if i do something like <a href="" /> it works great

i want to do the same in yii so i tried:


and it not working

so how do i make a link in yii and add in the end the # symbol?

$this->redirect (referring to the Controller) is used to automatically redirect an incoming request to a different url, which is a server-side action (not generating a clickable link on a web page), so cannot involve the hash part of the url.

If you mean that you want to generate a link in your view that the user can then click on, which should include a hash, then that is a different question.

I haven’t seen any parameters in createUrl for including a hash part, but at the very worst, in your view you would do something like

echo $this->createUrl('projects/index').'#example';

yes i want to redirect.

and tnx it works;


$url = Yii::app()->createUrl('projects/view')."&id=$model->projectID#uniqe_$id";      


its kind of a hack but it works

It doesn’t even need to be that hacky…

$url = Yii::app()->createUrl('projects/view', 'id' => $model->projectID)."#uniqe_$id";

should give you the same result, as well as allowing you to change your url scheme later without recoding if necessary (apart from the hash fragment of course).

And yes, I suppose if you are redirecting the url via a 302 code (not an invisible redirect), that should work - I hadn’t though about it like that.

thank you

and you forget the array(for anyone in the future that gonna search for that

$url = Yii::app()->createUrl('projects/view',array( 'id' => $model->projectID))."#uniqe_$id";

I am not sure if this is you’re looking for…

see this thread

In short,

Yii::app()->createUrl('projects/view', 'id' => $model->projectID, '#' => "#uniqe_$id");

Good to know - I hadn’t come across that before.

There is a small mistake in it.(extra # in value of # key)

And the correct one is
Yii::app()->createUrl(‘projects/view’, ‘id’ => $model->projectID, ‘#’ => “uniqe_$id”);