Chapter 3. Making Pretty URLs

This chapter explains how to configure URL rules and make URLs pretty, in particular for search engines. We will cover the following topics in this chapter:

  • Using Pretty URLs
  • Custom URL rules
    • Example – news items list by year or category
  • The default parameters in rules
    • Example – the index page to display list links
  • Complete URL rule parameters
  • The URL pattern to support a multilanguage view
  • Creating the rule class

Using pretty URLs

The URL format is very important for SEO. People do not pay attention to URLs (some browsers does not display them at all), but search engines make correspondences between text in the page and the URL.

Until now, we have used this type of URL index.php?r=site/index or index.php?r=site/about, where r indicates the parameter route to follow. Now, we will see how to change these formats in site/index and site/about, that are more easily readable and useful for search engines.

In order to use pretty URLs, we need to configure Yii2 to handle them, and this can be done in a couple of minutes.

First of all, we must ensure that all requests are rewritten to web/index.php. In Linux, we can change web server configuration using Apache and insert the .htaccess file in Yii2's app root folder, if this file does not exist. The .htaccess file allows us to override some default configuration of the web server.

Note

In the Linux environment, the filename starting with dot indicates that this file is hidden.

The content of .htaccess is the same as Yii1:

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 . web/index.php

If the app root is /var/www/vhosts/yiiapp/basic, we will insert .htaccess in /var/www/vhosts/yiiapp/basic.

The first row activates RewriteEngine of the web server; then, in the second and third rows, the script checks whether the request is not in an existing file or folder; and finally the request is rewritten to web/index.php. With these changes, all the requests that are not existing files or path folders will be rewritten to web/index.php.

Note

We can also configure rewrite rules in Apache configuration instead of the .htaccess file, if we have access to this level of Apache configuration.

If the .htaccess configuration has been ignored, check whether AllowOverride is set to All as follows:

<Directory /var/www/path/to/folder>
   AllowOverride All
</Directory>

And that is not set to None.

The last thing to do now is to configure Yii2 in order to handle a pretty URL.

Let's open config/web.php and add these contents in the components attribute:

'urlManager' => [
  'enablePrettyUrl' => true,
],

Adding the enablePrettyUrl property, we have just configured urlManager to enable the pretty URL, toggling the pretty URL format.

The previous URL index.php?r=site/index becomes /index.php/site/index and index.php?r=site/about becomes /index.php/site/about.

Using the enablePrettyUrl property, we will have the prefix index.php again. We can choose whether to keep it or not; however, to limit the URL length, it is advisable to remove it.

In order to control the presence of the index.php prefix, we use another property called showScriptName.

If we set this property to false, we will remove the first part of the URL. This is our updated configuration:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
],

Now, point the browser to http://hostname/basic/web/site/index to view the first page of the Yii2 application and check whether the other links are in the pretty format.

Finally, there is another property for the urlManager component, used to enable URL parsing based only on given URL rules, named enableStrictParsing. If this property is true, only the rules defined in urlManager will be executed; if there is no URL that matches the request, an error will be displayed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.51.3