Creating SEO friendly URL in Yii 2.0 framework

Yii 2.0 framework has a major improvement over version 1.1. Creating SEO friendly URL in Yii 2.0 framework has been a lot easier. All is just turning pretty url ‘on‘ to your Yii application and writing  very few rewrite rules in htaccess file, enough to hide index.php from your URL and to make them SEO friendly.

At the time to writing the post the Yii framework version 2.0 Beta has been released and I have used default installation to let these seo friendly URL work on local development server at port 8080. Follow this two steps article to turn pretty urls on.

Step 1: Define component in your config file

After successful installation of Yii, you can see application home page as given:

Yii seo friendly url

If you click on About link to visit about page in your site, your browser url will have index.php and get parameters according to path of your installation:

Go to config folder under your directory and modify $config array by setting  enablePrettyUrl to true and showScriptName to false for urlManager component.

Assigning showScriptName to false will hide index.php bootstrap file of Yii to appear in URL.

Step 2: Specify RewriteRule in htaccess

If you are at local web server then create a file called .htaccess, put the follow code into this and save this file under web folder in your application installation. For me this is htdocs > ymanual > basic > web. This will force every request to pass through index.php bootstrap file of Yii except except static resources like images.

On a production server you should made the following configuration in Apache’s httpd.conf file or within a virtual host configuration.

Necessarily replace path/to/basic/web with your actual for basic/web.

After making those adjustment you can access your site with SEO friendly URL in Yii 2.0 as given:

 

Like this Post? Share