【问题标题】:User friendly urls revelsal用户友好的网址显示
【发布时间】:2012-09-24 15:36:03
【问题描述】:

假设我想将http://example.com/topic/view/topicname/thenameofthetopic 直接显示为http://example.com/thenameofthetopic

所以我已经实现了,当输入 http:// example.com/thenameofthetopic 时,它会将您正确重定向到控制器/动作,并且网站显示我想要显示的内容,问题是当然,URL 改变了,我想要它保持格式http://example.com/thenameofthetopic

这可能不会触及 htaccess 文件吗?只需用 yii 配置即可

我该怎么做?

【问题讨论】:

标签: redirect yii routes friendly-url


【解决方案1】:

正是你的情况:

'components'        => array(
  'urlManager' => array(
      'urlFormat'      => 'path',
      'showScriptName' => false,
      'rules' => array(
          "<name_of_topic:\w+>/" => 'controller/action'
       ),
   ),
)

你会在 Yii::app()->request->getQuery('name_of_topic'); 中得到主题名称;

htaccess 是 Yii - 经典,必须存在:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]

我建议你换一种方式。

'components'        => array(
  'urlManager' => array(
      'urlFormat'      => 'path',
      'showScriptName' => false,
      'rules' => array(
          "t/<name_of_topic:\w+>/" => 'controller/action'
       ),
   ),
)

所以主题链接将是http://www.example.com/t/name_of_topic。 这将允许您对文章等使用“a//”。

还有一件事。阅读这篇文章,发现很多有趣的事情http://www.yiiframework.com/doc/guide/1.1/en/topics.url

【讨论】:

  • 我之前修改了htacces,如果我添加你的行,我得到的网站没有格式...这是我的实际.htaccess文件RewriteEngine on #如果目录或文件存在,直接使用它RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # 否则转发给 index.php RewriteRule 。 index.php
  • 抱歉,“获取没有格式的网站..”是什么意思?
  • 你的 htaccess 没问题。正如这里写的yiiframework.com/doc/guide/1.1/en/topics.url。希望 urlManager 设置对你有所帮助。
  • 非常感谢!!昨天太晚了,我无法清晰地思考......但现在我已经按照你的步骤,它工作了!!!
猜你喜欢
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
  • 2013-03-23
  • 2012-10-15
  • 2011-02-15
  • 1970-01-01
  • 2016-03-11
  • 1970-01-01
相关资源
最近更新 更多