user2636556
user2636556

Reputation: 1915

yii removing sub folders from url string

I'm trying to remove /site/ and /user/ from my url's on Yii. i tried adding this code

'contact' => array('site/contact', 'caseSensitive'=>false),

but i still can't get it to work. I'm trying to get all my static pages to look like mydomain.com/about.html

here are some of the url's i want to change

/site/page?view=about ==> mydomain.com/about.html
/site/contact.html ==> mydomain.com/contact.html
/user/login.html ==> mydomain.com/login.html

here is want i got so far

 'urlManager'=>array(
                'urlFormat'=>'path',
                'urlSuffix'=>'.html',
                'rules'=>array(
                    '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                    '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                    '<action:.*>'=>'site/<action>',
                    'user/<controller:\w+>/<action:\w+>'=>'user/<controller>/<action>',
                ),
                'showScriptName' => false,
            ),

this is my .htaccess

# Turning on the rewrite engine is necessary for the following rules and features
# FollowSymLinks must be enabled for this to work
<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
</IfModule>

# Unless an explicit file or directory exists, redirect all request to Yii entry script
<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . index.php
</IfModule>

Upvotes: 0

Views: 910

Answers (1)

ezze
ezze

Reputation: 4110

It seems that some rule staying before your rules for main and user is applied. The third one '<controller:\w+>/<action:\w+>'=>'<controller>/<action>' always matches any route and preserves ids of controller and action in URL. Since first matched rule is applied all the following rules doesn't make sense.

You should move your rules to the top of rules array of CUrlManager. Supposing main and user are ids of your controllers they could look like this:

'rules' => array(
    '<view:(about)>' => 'site/page'
    '<action:(contact)>' => 'site/<action>',
    '<action:(login)>' => 'user/<action>',
    // the rest rules here
)

If you want to use site/page route not only for about view you should follow parameterizing routes technique. For example:

'<view:(about|guestbook|download)>' => 'site/page'

Using something like '<view:\w+>' => 'site/page' instead will also convert /contact.html to /site/page?view=contact and /login.html to /site/page?view=login. As the result, all other rules that match these URLs will never be applied that's not acceptable for you.

Upvotes: 1

Related Questions