Scriptbaker
SCRIPTBAKERAI & Software Engineering
Yii

Remove index.php from URL in Yii

Learn how to configure Yii's UrlManager and Apache .htaccess rules to create clean, user-friendly URLs. This guide covers URL configuration, rewrite rules, common issues, and practical tips for improving Yii application routing

· 5 min read · By Tahir Yasin

When building interactive web applications, you often need to detect when a user has finished typing before executing a function. A common example is a search box where you want to wait until the user pauses before sending a request to the server.

How to Set Up Yii URL Manager and Clean URLs

Yii's URL manager can make application URLs cleaner and easier to read. Instead of exposing index.php and query parameters in every URL, you can configure Yii to use more user-friendly routes.

The setup requires two main changes: enabling the UrlManager component in your Yii configuration and adding an .htaccess file to the application's root directory.

Step 1: Enable the UrlManager

Open protected/config/main.php in your Yii application and locate the URL manager configuration. If it is commented out, uncomment it so Yii can process clean application routes.

Depending on your Yii version and configuration, the component may look similar to:

'urlManager'=>array(    'urlFormat'=>'path',    'showScriptName'=>false,),

The urlFormat option enables path-style URLs, while showScriptName can hide index.php from generated URLs when the server rewrite configuration is working correctly.

Step 2: Create the .htaccess File

Create a file named .htaccess in the root directory of your Yii application. Add the following Apache rewrite rules:

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

These rules allow Apache to send requests that do not match an existing file or directory to Yii's front controller, index.php. Yii can then determine which controller and action should handle the requested URL.

Why Use Clean URLs?

Clean URLs are easier for users to understand and can make an application look more professional. They can also be easier to share, read, and maintain than URLs containing long query strings.

For example, an application can use a route such as /products/books instead of exposing implementation details in the URL.

Common .htaccess Problems

If the rewrite rules do not work, check that Apache's rewrite module is enabled and that the server permits overrides through .htaccess. On Apache servers, the relevant directory configuration generally needs to allow rewrite rules.

Also make sure the .htaccess file is placed in the correct application root and that the file is actually being read by Apache. A server configuration issue can prevent Yii from receiving rewritten requests.

Frequently Asked Questions

What is Yii UrlManager?

Yii UrlManager is a Yii component used to create and interpret application URLs. It can be configured to use different URL formats and routing rules.

Where should the .htaccess file be placed?

For this setup, place the .htaccess file in the root directory of the Yii application so Apache can apply the rewrite rules to incoming requests.

Why is index.php still appearing in my URLs?

Check that showScriptName is configured appropriately, the Apache rewrite module is enabled, and the .htaccess rules are being applied by the server.

Does this .htaccess configuration work on every server?

The example is intended for Apache servers that support mod_rewrite and allow directory-level rewrite rules. Other web servers may require different rewrite configuration.

Can clean Yii URLs improve usability?

Yes. Descriptive URLs can make links easier to read, understand, copy, and share. They also help communicate the structure of an application to users.

Conclusion

Configuring Yii's UrlManager together with Apache rewrite rules is a straightforward way to create cleaner application URLs. Enable the URL manager in protected/config/main.php, add the appropriate .htaccess rules, and verify that Apache is configured to process rewrites.

Last updated:

Work with Scriptbaker

Let's talk about your project

Whether you're extending an existing platform, modernising legacy code, or planning something new, our engineers can help. Tell us what you're working on and we'll explore a practical technical solution together.