Kyle
Kyle

Reputation: 745

Yii2 using pretty url returns 404

I've set up a few Yii projects and never had this problem. I'm using the basic template.

Apache config:

<VirtualHost *:80>
ServerName shop
DocumentRoot "C:/xampp/htdocs/shop/web"

<Directory "C:/xampp/htdocs/shop/web">
 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

# if $showScriptName is false in UrlManager, do not allow accessing URLs with script name
RewriteRule ^index.php/ - [L,R=404]

# ...other settings...
</Directory>

</VirtualHost>

URL manager app/config/web.php:

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

     ],
],

Whats weird is that it lets me access shop/gii and shop/product but if I try to navigate anywhere else I get a

Error: 404 object not found.

If I remove the UrlManager config everything works as expected.

Upvotes: 0

Views: 1053

Answers (1)

Muhammad Omer Aslam
Muhammad Omer Aslam

Reputation: 23778

You need to remove the line

RewriteRule ^index.php/ - [L,R=404]

That is what is making the problem and not allowing you to access the routes, i normally use the following config and never had any problem.

<VirtualHost *:80>
       ServerName www.virtualdomain.local
       DocumentRoot "F:\xampp\htdocs\someproject\frontend\web"

       <Directory "F:\xampp\htdocs\someproject\frontend\web">
           # use mod_rewrite for pretty URL support
           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

           # use index.php as index file
           DirectoryIndex index.php

           # ...other settings...
       </Directory>
   </VirtualHost>

Upvotes: 1

Related Questions