Yii 2 Configuration for Htaccess

The Apache documentation recommends placing RewriteRule and the in the server configuration, however, in shared hosting situations, they can also be put in htaccess. As a web designer and developer, I have limited experience with server rules.


Question:

As I am currently developing a Yii2 Project, I have ensured its proper setup on
Wamp Localhost
. Additionally, I have established an alias on my Wamp.

Alias /project "c:/Users/Shaun/Desktop/projects/project/web/" 

    AllowOverride all

I have put
htaccess file
in the web directory of a simple application, where it is currently located.

project/web/.htaccess

project/web/index.php

project/web/index-test.php

I have verified that mod_rewrite is enabled and I am able to access my root directory without any difficulties.

Here’s the htaccess file I’m using, as shown in the Yii2 documentation.

RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
http://localhost/project/

Upon accessing the URL mentioned, I am able to successfully reach the Index action of my SiteController. However, if I attempt to access another page such as:

http://localhost/project/site/index

It states:

The server could not locate
requested URL
/Users/Shaun/Desktop/projects/project/web/index.php.

Is it present for sure? As a component in my web.php configuration, I have enabled it.

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

Despite my inability to locate it, implementing this method displays the routes without any issues.

http://localhost/project/index.php/site/index

This is really annoying, I have no desire for this. What’s going on?

Thanks,


Solution 1:

As you are dealing with the virtual directory, where an alias (e.g. /site/) is mapped to an actual directory (e.g. /dir/), it is necessary to utilize.

RewriteBase /site/

in your apache configuration file.


Solution 2:


Does the web folder contain any additional project directories? The format of your current project appears to be atypical.

In case your response is affirmative, it is necessary to place the .htaccess file within this particular project directory.

However, having the web in the project folder is uncommon. Nonetheless, if this is the case, the .htaccess file must still be located in the project folder.


Solution 3:


To enable pretty URLs, it is necessary to place the

AllowOverride All

setting in either httpd.conf or your vhost conf.


Solution 4:

You need to modify three files, namely the .htaccess in the “basic” folder, the .htaccess in the “web” subfolder of “basic”, and the “web.php” file in the “config” subfolder of “basic”.

Utilize the code provided in the following URL.

Check out this resource on how to use htaccess in a Yii2 basic application, available at codematrics.com.

This will help you.

Frequently Asked Questions