0

I need to rewrite the following URLS

site.com/events/?event=test

to read like

site.com/events/test

I'm fine with doing this either in htaccess or in the urlManager.

0

3 Answers 3

1

If /events is your controller class, and /test is your parameter for the index action:

class EventController extends Controller {

    public function actionIndex($event) {

Add this rule to your urlManager:

'urlManager' => array(
    'urlFormat' => 'path',
    'rules' => array(
        'event/<event>' => 'event/index',

        ...

This will automatically map test in the url /events/test to the parameter $event in the index action.

    public function actionIndex($event) {

        echo $event; // produces "test"
Sign up to request clarification or add additional context in comments.

Comments

0

htaccess should be:

<ifModule mod_rewrite.c>
# Turn on the engine:

RewriteEngine on

# Do not perform redirects for files and directories that exist:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# For everything else, redirect to index.php:

RewriteRule . index.php
</ifModule>

urlmanager should be:

'urlManager'=>array(
        'showScriptName'=>false, // removed index.php with .htaccess
        'urlFormat'=>'path',
        'rules'=>array(
           'test' => 'site/test' // Where controller/view
    ),
),

Comments

0

This can be done on htaccess with the following:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^event=([^/]+)$
RewriteRule ^events/$ /events/%1/? [R=302,L]

The above should work (changing R=302 to R=301 when you are sure the redirect works properly).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.