1

我在 yii 框架中工作并尝试使用 htaccess 文件重写 url 这是我的文件(第一步我试图使用一个简单的规则)

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^carguide$  MainModel/index/304 [NC,L]

# otherwise forward it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php


# otherwise forward it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

我想将 url carguide 重写为 mainModel/index/304,但是我尝试过的 yii 没有收到原始 url 并引发 404 错误。

这是日志文件:

strip per-dir prefix: C:/xampp/htdocs/cars/carguide -> carguide
applying pattern '^carguide$' to uri 'carguide'
RewriteCond: input='C:/xampp/htdocs/cars/carguide' pattern='!-f' => matched
RewriteCond: input='C:/xampp/htdocs/cars/carguide' pattern='!-d' => matched
rewrite 'carguide' -> 'MainModel/index/304'
add per-dir prefix: MainModel/index/304 -> C:/xampp/htdocs/cars/MainModel/index/304
strip document_root prefix: C:/xampp/htdocs/cars/MainModel/index/304 -> /cars/MainModel/index/304
internal redirect with /cars/MainModel/index/304 [INTERNAL REDIRECT]
add path info postfix: C:/xampp/htdocs/cars/MainModel -> C:/xampp/htdocs/cars/MainModel/index/304
strip per-dir prefix: C:/xampp/htdocs/cars/MainModel/index/304 -> MainModel/index/304
applying pattern '^carguide1$' to uri 'MainModel/index/304'
add path info postfix: C:/xampp/htdocs/cars/MainModel -> C:/xampp/htdocs/cars/MainModel/index/304
strip per-dir prefix: C:/xampp/htdocs/cars/MainModel/index/304 -> MainModel/index/304
applying pattern '.' to uri 'MainModel/index/304'
RewriteCond: input='C:/xampp/htdocs/cars/MainModel' pattern='!-f' => matched
RewriteCond: input='C:/xampp/htdocs/cars/MainModel' pattern='!-d' => matched
rewrite 'MainModel/index/304' -> 'index.php'
add per-dir prefix: index.php -> C:/xampp/htdocs/cars/index.php
strip document_root prefix: C:/xampp/htdocs/cars/index.php -> /cars/index.php
internal redirect with /cars/index.php [INTERNAL REDIRECT]
strip per-dir prefix: C:/xampp/htdocs/cars/index.php -> index.php
applying pattern '^carguide$' to uri 'index.php'
strip per-dir prefix: C:/xampp/htdocs/cars/index.php -> index.php
applying pattern '.' to uri 'index.php'
RewriteCond: input='C:/xampp/htdocs/cars/index.php' pattern='!-f' => not-matched
pass through C:/xampp/htdocs/cars/index.php

我试图更改标志,添加一个 RewiteBase。

4

1 回答 1

1

正如我所看到的,您不需要使用.htaccess为您网站的每个页面创建特定的重写规则。使用这里.htaccess建议的 simple 就足够了,然后使用 Yii 的URL 管理器配置你的重写规则。

更新

如果您的控制器名称是mainModel并且您的操作名称是index(就 Yii 而言,路由是mainModel/index)并且您希望可以通过此页面访问此页面,http://yoursite/carguide那么您的 URL 管理器配置protected/config/main.php可能如下所示:

'urlManager' => array(
    'urlFormat' => 'path',
    'showScriptName' => false,
    'rules' => array(
        'carguide' => 'mainModel/index',
    )
),

更新

有关Yii 的 URL 管理器类支持的配置属性,请参阅此页面。

于 2013-02-03T14:15:16.913 回答