Clean URL's Not clean enough

Hi all,

Any tips on how I can change…

http://localhost/MyApplication/site/page/view/about

to

http://localhost/MyApplication/about

and etc…

I know this can be done with mod_rewrite but then how about in Yii where the menu items / links are created?

Thanks for the advice!

http://www.yiiframework.com/doc/guide/1.1/en/topics.url

Thanks!

That doen’t really explain it though. I can’t find a usage example. I’m also stumped for a usage example of:


Yii::setPathOfAlias('aboutus','/protected/views/cashier');

And also on the topic of URL’s…

How about renaming the SiteController.php - so that we don’t have any /site/etc in the URL’s

I know much of this is mod_rewrite, however the paths inside Yii must need fixing to accomodate all of these things. I think the documentation is great but there realy are very few examples . I hope you can shed some light in these issues!

Thanks again,

Dan

You can do this: “<view:(page1|page2|…)>” => ‘site/page’

Thanks Qiang,

Is that to go in the main configuration file? Any example?

:)

It goes to urlManager rules array.

Thanks Sam,

Here it is so others can more easily follow…

To get rid of the site/whatever in the URL’s make sure the following are in place:

  1. In protected/config/main.php try the following code:



'urlManager'=>array(

'urlFormat'=>'path',

'showScriptName' => false, 

'rules'=>array(

'<view:(contact-us)>' => 'site/contact',			

), 

),



In the example above the URL yoursite.com/yourapplication/site/contact will now be able to access as yoursite.com/yourapplication/contact-us (the "site/" has been removed).

You can add more rules like so:




'urlManager'=>array(

'urlFormat'=>'path',

'showScriptName' => false, 

'rules'=>array(

'<view:(about)>' => 'site/page/view/about',

'<view:(page1)>' => 'site/page/view/1',

'<view:(page2)>' => 'site/page/view/3',			

), 

),



Note: <view:(contact-us)> accepts regular expressions if you like to use them.

  1. Make sure your .htaccess in your application root looks like as follows:



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



You should now have clean URL’s and be able to change the URL’s as you like.

I think that, as Quiang adviced, something like that should work:




'urlManager'=>array(

'urlFormat'=>'path',

'showScriptName' => false, 

'rules'=>array(

'<view:(about|page1|page2)>' => 'site/page',

), 

),



Yes it does in the right context, thanks.

I’m an drupal developer, and drupal does this by rewriting the urls with an .htaccess file. Changed a part of this .htaccess file to use this within my yii setup.

<IfModule mod_rewrite.c>

RewriteEngine on

Rewrite URLs of the form ‘x’ to the form ‘index.php?r=x’.

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} !=/favicon.ico

RewriteRule ^(.*)$ index.php?r=$1 [L,QSA]

</IfModule>

I’m pretty new to Yii, and PHP as well. I’ve been trying to implement this in a project, but when I put "‘showScriptName’ => false, " into my urlManager, I end up getting 404 Not Found errors. I’ve followed along with this post and have pretty much copy and pasted the .htaccess and the urlManager array, making minor tweaks in the array so the names match up with my pages, which are mostly the ones you generate in the tutorial (i.e. “testdrive”): about, contact, etc… Any ideas why this would be happening?

Thanks,

Alex

make sure apache’s mod_rewrite is enabled

I tried that following this tutorial: http://www.tutorio.com/tutorial/enable-mod-rewrite-on-apache

Then I restarted all services on my wamp server. Still having 404 Not Found errors. I can login fine, but once I click "Contact", "About", or any other links, the error pops up.

did you set your configuration’s like the doc ?

mine is the following:




'components'=>array(

	   //....

    	'urlManager'=>array(

    		'urlFormat'=>'path',

        	'showScriptName'=>false,

        	'caseSensitive'=>false,

        	'matchValue'=>false,

   	)



I had something similar. So for good measure, I copied yours into my code. Still the same thing: "The requested URL /mysite/site/contact was not found on this server."

I’ve never used it like this

"/mysite/site/contact"

my projects are always like

"/site/contact"

sorry cant help you more , some1 that has done it already should post something

Since you haven’t exactly posted anything, it could be anything…

Why don’t you start by quoting the url rules?

1. Start with a check that the urlManager works

(enter url in browser address field)




...yoursite/index.php/controller

and

...yoursite/index.php/controller/action



2. and that showScriptName=false and mod_rewrite works




...yoursite/controller/action



3. In some cases modify links to use urlmanager, e.g.




Yii::app()->urlManager->createUrl('site/page', array('view'=>'about'))



(CMenu normally should do this IIRC)

/Tommy

@jacmoe

Right now, I don’t have any rules. My current urlManager looks like this:




'urlManager'=>array(

     'urlFormat'=>'path',

     //'showScriptName' => false, 

     //'caseSensitive'=>false,

     //'matchValue'=>false,


     //'rules'=>array(

          //'<view:(about|contact)>' => 'site/page',

	  //'<view:(index)' => 'site/login',

     //), 

),



It might also be helpful for me to mention that the way my site works is that you have to go through the login page to access any other part of the site (which is why I have <view:(index)’ => 'site/login)

@tri

I tried going through your steps. Step 1 works for my pages like contact. However, controller and controller/action give me 404 errors.

From what I can tell, mod_rewrite should be enabled. I went into httpd.conf and uncommented:




LoadModule rewrite_module modules/mod_rewrite.so



I also went into wamp’s .htaccess (on my computer, located in C:\wamp) and put the following as the only text in it:




Options +FollowSymLinks

RewriteEngine On



Also, when I uncomment “‘showScriptName’ => false,” in my main.php, I can login fine, but any link to another page in the site will give me errors.

@Gustavo

Thanks for all the help, suggestions and taking the time.

Just to clarify

controller: e.g. ‘site’

controller/ation: e.g. ‘site/index’

still get 404 error?

I use the same .htaccess as in post #7.

You may need to change AllowOverride (to All or possibly Options) in httpd.conf. Check the Apache error log for messsages like "Options not allowed"

So if my step 2 succeeded (e.g. yoursite/site/login) the rewrite actually is in effect (no need to change AllowOverride). But I don’t understand whether you can access other pages from the browser address field (e.g. yoursite/site/index).

The rest is what I mentioned in my step 3, try to add such links in a view. Then fix the menu links (e.g ‘/controller/action’) or possibly use createUrl().

/Tommy