0

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>',
        ),
    ),
mabarroso
  • 659
  • 2
  • 11
  • 23
martyn
  • 230
  • 5
  • 22
  • as a side note, this .htaccess is not necessary the best. it lets people "see" something when you have a .git folder, or .netbeans or anything... of course, you should not have anything like that. But... I am trying this at the moment : RewriteEngine on RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php|ico|txt)$ RewriteRule . /index.php [L] (a .htaccess with 3 lines) – Cedric Jun 26 '13 at 00:12

1 Answers1

2

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...

ews2001
  • 2,176
  • 3
  • 26
  • 38
  • Thanks, it works! It seems the '' => 'site/index' was the issue. I actually do have it set up as http:// mytestapp/ – martyn Aug 31 '12 at 08:58
  • was too long editing this. Thanks, it works in a way. The index.php/ads still resolves as well as /ads. Is there a way to stop that? and http://yii.dev:8888/ads/business works but http://yii.dev:8888/ads/seller resolves to the server address http://yii.dev which doesn't work. looking into it now. – martyn Aug 31 '12 at 09:15
  • As far as having the URL NOT work with the index.php in it...well, when you visit the Yii Framework site, it works that way as well (http://www.yiiframework.com/index.php/about/). I guess you could write something that stripped it from the URL and redirected, but I don't see the purpose... Also, did you have a question in your last sentence? At the moment, it doesn't make much sense :) – ews2001 Aug 31 '12 at 19:59
  • Was just concerned about duplicate content. As I always prefer one url to one page if possible. The last part was an issue where yii.dev:8888/ads/seller redirects to yii.dev so for some reason the code you gave does not like yii.dev:8888/ads/seller but yii.dev:8888/index.php/ads/seller works. – martyn Sep 03 '12 at 14:10
  • As far as duplicate content is concerned, just make sure ALL of your links don't have index.php in the URL path. For the other question, maybe you could add the urlManager array from your main.php in config...I have the same type of setup and none of my pages (including redirected ones) need to include index.php in the path. – ews2001 Sep 04 '12 at 19:07