How to configure SEO friendly PrettyUrl in Yii2?
We can easily configure Yii2 web applications URL SEO friendly by following below steps-
Step 1: Add urlManager in config/web.php
Step 2: Add .htaccess in web/ directory
Step 3: Check virtual host setup in httpd-vhosts.conf file
Step 1: Add urlManager in config/web.php
Add the following code in config/web.php ‘components’ section and save the file. For Yii2 advanced setup please go to \common\config\main.php file and add in components’ section.
'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, ],
Step 2: Add .htaccess in web/ directory
Create .htaccess file inside web/ directory with following text-
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
Step 3: Check virtual host setup in httpd-vhosts.conf file
Go to extra directory i.e. C:\wamp\bin\apache\apache2.4.9\conf\extra and then open httpd-vhosts.conf. Check AllowOverride value setup as all settings inside <VirtualHost *:80> </VirtualHost>.
DocumentRoot “c:/wamp/www/mysite”
ServerName mysite.dev
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
Check and confirm the file before closing. Now restart Apache server and check web URL i.e. mysite.dev/site/about
Enjoy exploring Yii2!