关于不使用.htaccess文件达到隐藏入口文件目的的问题

我自己的项目(使用YII)在尝试隐藏index.php文件的时候发现老是报不能确定入口文件,事实上我在HTTPD.CONF文件中采用了重写规则定位到入口文件,例如 http://www.yii.com/do/post/add  会被重写为 http://www.yii.com/d...x.php/post/add

你能告知print_r($_SERVER)的结果么?

你可以在app config里通过配置request的scriptUrl属性来强制设定entry script URL.

强哥你好, 假设请求的URL为 http://www.yii.com/do/post

httpd.conf中在相关配置为

<VirtualHost 127.0.0.1> 

ServerName www.yii.com

DocumentRoot o:/www/yii

RewriteEngine  on

RewriteRule ^/do/(.*)$ /do/index.php/$1 [L]

</VirtualHost>

URL最后会被重写为http://www.yii.com/do/index.php/post

此时

$_SERVER['SCRIPT_FILENAME']='O:/www/yii/do/index.php'

$_SERVER['SCRIPT_NAME']='/do'

$_SERVER['PHP_SELF']='/do/post'

$_SERVER['ORIG_SCRIPT_NAME']=null

basename($_SERVER['SCRIPT_FILENAME'])='index.php'

basename($_SERVER['SCRIPT_NAME'])='do'

basename($_SERVER['PHP_SELF'])='post'

因此最后出现上面我提到的错误

最后我在httpd.conf中去掉VirtualHost中的重写规则,转而采取如下配置

<Directory O:/www/yii/do>

    #Options +FollowSymLinks

    IndexIgnore /

    RewriteEngine on

    # if a directory or a file exists, use it directly

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    # otherwise forward it to index.php

    RewriteRule . index.php

</Directory>

此时,通过http://www.yii.com/do/post 可以访问到正确的控制器post

Quote

另一个问题是我在配置文件中main.php将'showScriptName'=>'false'后,发现使用$this->createUrl产生的URL依然有index.php,我使用自带的博客示例做实验,仍然如此.

'urlManager'=>array(

		&#039;urlFormat&#039;=&gt;&#039;path&#039;,





	&nbsp; &#039;showScriptName&#039;=&gt;&#039;false&#039;,





		&#039;rules&#039;=&gt;array(





	&nbsp; &nbsp; &#039;posts&#039;=&gt;&#039;post/list&#039;,





			&#039;post/&lt;id:\d+&gt;&#039;=&gt;&#039;post/show&#039;,





			&#039;post/update/&lt;id:\d+&gt;&#039;=&gt;&#039;post/update&#039;,





			&#039;tag/&lt;tag&gt;&#039;=&gt;&#039;post/list&#039;,





		)</div></div>

呵呵,原来是由于 'showScriptName'=>'false',  昨晚弄太晚了,没仔细看,去掉引号就行了 呵呵

这个改写后的结果的确比较怪。不过scriptUrl的设置就是为了对付这种框架无法正确判断的情形的。

哦 我昨晚试验了很久 当时也是很郁闷 呵呵  强哥回复真及时呀  :D