htaccess RewriteRule不起作用

时间:2013-02-03 13:49:42

标签: .htaccess mod-rewrite yii

我在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收到orliginal网址并提出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。

1 个答案:

答案 0 :(得分:1)

我可以看到您不需要使用.htaccess为网站的每个页面创建特定的重写规则。只需使用简单的.htaccess作为建议的here,然后使用Yii的URL manager配置重写规则。

<强>更新

如果您的控制器名称为mainModel且您的操作名称为index(就Yii而言,路线为mainModel/index)并且您希望此页面可通过{{1}访问那么http://yoursite/carguide中的URL管理器配置可能如下所示:

protected/config/main.php

<强>更新

有关Yii的URL管理器类支持的配置属性,请参阅this page