Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the coder-elementor domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u262393194/domains/codestap.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rank-math domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u262393194/domains/codestap.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rocket domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u262393194/domains/codestap.com/public_html/wp-includes/functions.php on line 6114
How do you configure Yii’s "URL Manager" for SEO-friendly URLs? - Code Stap
How do you configure Yii’s “URL Manager” for SEO-friendly URLs?

How do you configure Yii’s “URL Manager” for SEO-friendly URLs?

Configuring Yii’s URL Manager for SEO-friendly URLs involves setting up pretty URLs and defining custom URL rules in your configuration file. Here’s a concise guide on how to do this:

Minimal Steps to Configure Yii’s URL Manager for SEO-Friendly URLs

Enable Pretty URLs:

    • Open the main configuration file, typically found at config/web.php, and enable pretty URLs.

Example

<?php
'components' => [
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false, // Hides index.php from URLs
    ],
],
?>

Define Custom URL Rules:

  • Add custom URL rules to map URLs to controllers and actions. This allows you to create more readable and SEO-friendly URLs.

Example

<?php
'components' => [
    'urlManager' => [
        // Previous configuration...
        'rules' => [
            'about' => 'site/about', // Maps /about to SiteController's actionAbout
            'contact' => 'site/contact', // Maps /contact to SiteController's actionContact
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>', // Standard rule for controller/action/id
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>', // Standard rule for controller/action
            // Add more rules as needed
        ],
    ],
],
?>

Create .htaccess for Apache (if using Apache):

  • To ensure that the server handles requests correctly, create or update the .htaccess file in your web root directory.

Example .htaccess file:

Example

RewriteEngine on

# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward the request to index.php
RewriteRule . index.php
  • Access SEO-Friendly URLs:

    • You can now access your application using SEO-friendly URLs. For example:
      • /about will direct to SiteController::actionAbout().
      • /contact will direct to SiteController::actionContact().
  • Test Your URLs:

    • Test the URLs in your browser to ensure they are working as intended and redirecting to the correct actions.

Related Questions & Topics