yii, mamp, htaccess rewrite urls

孤街浪徒 提交于 2019-12-11 18:18:53

问题


I am trying to rewrite yii urls but with no luck. I have spent hours going through sites and come up with the same answer each time which doesn't work for me:

http://www.yiiframework.com/doc/guide/1.1/en/topics.url#user-friendly-urls

I would like to resolve the urls to basic paths e.g

/index.php/site/index to /

/index.php/ads and /index.php/ads/ to /ads

/ads/details?ad=9 to /ads/9

The problem seems to be that the .htaccess has no effect.

I am using:

mamp pro on a mac with lion and the web directory is different to the webserver root. I have set AllowOveride through the console.

The .htaccess is in the same folder as the main index.php but doesn't register even if I create an error.

I have had no problem with other non-yii web directories using an htaccess file

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php

//main.php

  'urlManager'=>array(
        'urlFormat'=>'path',
                    'showScriptName'=>false,

        'rules'=>array(

                        '<controller:\w+>/<id:\d+>'=>'<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
        ),
    ),

回答1:


I have a project where my Yii application is in a subfolder as well.

This is my .htaccess file:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]

Then in main.php, you'll need to add these lines to the 'rules' array:

'' => 'site/index', // This is for the home page

Is ads the name of your controller? In other words, do you have a file named AdsController.php in your controllers folder? If so, then it will work by default.

Just curious why you're hitting the index.php directly? You should be able to setup your localhost to point directly to your Yii app folder. For example, on my machine I created http:// mytestapp/ and it points to the app's folder in htdocs.

Since you're on a Mac, you need to edit your etc/hosts file as well...

I added a line like this: 127.0.0.1 mytestapp

Hope that helps...



来源:https://stackoverflow.com/questions/12202890/yii-mamp-htaccess-rewrite-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!