Htaccess Yii 2 Configuration

2019-06-01 01:07发布

问题:

I am working on a Yii2 Project and have set it up on Wamp Localhost. My Wamp I have set up an alias:

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

<Directory "c:/Users/Shaun/Desktop/projects/project/web/">
    AllowOverride all
</Directory>

In a basic app I have placed the htaccess file into the web directory (where it is currently pointing to)

project/web/.htaccess

project/web/index.php

project/web/index-test.php

mod_rewrite is definitely enabled I have checked. I can navigate to my root without a problem:

This is my htaccess file as demonstrated on Yii2 docs.

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/

When I navigate to url above I hit my SiteController Index action fine. But when I try to go to for example:

http://localhost/project/site/index

It states:

The requested URL /Users/Shaun/Desktop/projects/project/web/index.php was not found on this server.

But it is definitely there?! If you see my web.php configuration as a component I have enabled:

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

Still can't find it. But if I do this it shows the routes fine:

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

So frustrating as I don't want this. What's the deal here?

Thanks,

回答1:

Because you are working on the virtual directory (e.g. Alias /site/ "/dir/"), you must use

RewriteBase /site/

in your apache configuration file.



回答2:

The structure of your Project seems a bit unsual. Is there another project folder in the folder web ?

If the answer is yes, you have to put the .htaccess file in this project folder.

But if the project folder contains web, it's quite unsual. But also in that case, the .htaccess file has to be in the project folder.



回答3:

Set AllowOverride All in httpd.conf or your vhost conf.
This is mandatory to work with pretty URLs.



回答4:

You have to change three files.
basic > .htaccess
basic > web > .htaccess
basic > config > web.php

Use the code given in the below url.

http://www.codematrics.com/htaccess-in-yii2-basic-app/

This will help you.