1

I configured UrlManager in a project and It was working the way I wanted. Now i tried to add a content whose name contains a trailing slash but i get an error 404 (Object not found).

For example: www.test.com/article/detail/id_of_article/title_of_article

title_of_article = People are ... => Work

title_of_article = 1/3 of People are ... => Doesn't Work (Object not Found)

The Trailing Slash is breaking the Url although it is encoded in %2F

This is how i create url:

Html::a(Html::encode($model->title), 
        ['article/detail', 'id' => $model->id, 'title' => $model->title])

I don't really know how I can deal with that.

3 Answers 3

1

For This , the best solution is to use slug names.

Instead of id and title, take one more field called slug_name in your database.

On Add or update of any record generate slug name and store in db.

For generating slug name, you can use custom function as below.

public function getSlugName($id,$title)
  {
    $slug=$id;
    if(isset($title) && $title!=null)
    {
      // remove all spacea
      $slug.='-'.str_replace(' ','-',strtolower($title)); 
    }
    $slug=preg_replace('/[^A-Za-z0-9\-]/', '', $slug); // Removes special chars.
    $slug=str_replace(array('--'), '-', $slug); // remove multiple --
    return $slug;
  }

This function will return you uniq name. So you can use it in url.

This is also help in SEO.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a good idea. It s true it will change the title name a litte bit by removing special chars, but it is an good alternative.
1

Could be you need URL normalization

Since version 2.0.10 UrlManager can be configured to use UrlNormalizer for dealing with variations of the same URL, for example with and without a trailing slash.

NB by default UrlManager::$normalizer is disabled. You need to explicitly configure it in order to enable URL normalization.

see how here http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#url-normalization

1 Comment

@stfsngue : you mean it can be seen as duplicated content?
0

There is an encodeParams property of UrlRule. Please try with that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.