rji rji
rji rji

Reputation: 707

Redirection using Routing not working in yii2

I have ProjectsController.php in which I have actionIndex

In my main.php file which is in frontend/config/main.php I have done routing as below.

 'components' => [
    'urlManager' => [
        'showScriptName' => false,
        'enablePrettyUrl' => true,
        'rules' => [
            'projects'=>'projects/index',
         ],
       ],  
     ]

Which means I am trying to redirect to index action in projects controller with url /projects. I am able to access in my localhost like localhost/myproject/projects, but whereas when I try to access the same in live like www.myproject/projects/ it is redirecting to home page and if I try to access like www.myproject/frontend/web/projects/index then I am able to redirect to the correct page.Not only this particular action and controller, all the other routing mentioned in main.php are redirecting to home page.

Below is my .htaccess file code in my root file '/'

    # prevent directory listings
#Options -Indexes
#IndexIgnore */*

# follow symbolic links
#Options FollowSymlinks
RewriteEngine on
#RewriteCond %{REQUEST_URI} ^/common/css/
#RewriteRule ^common/css/(.*)$ common/css/$1 [L]
#RewriteCond %{REQUEST_URI} ^(?!.*/web/)
#RewriteCond %{REQUEST_URI} !^/web/
#RewriteRule .* backend/web/index.php [L]
#RewriteCond %{REQUEST_URI} ^(?!.*/web/)
#RewriteRule ^(.*)?$ backend/web/$1 [L,PT]

#RewriteCond %{REQUEST_URI} !^/frontend/web/
#RewriteRule backend/(.*)$ backend/web/index.php [L]
#RewriteRule ^frontend/(.*)$ frontend/web/$1 [L]
#RewriteBase /
RewriteRule ^common/(.*)$      common/$1   [QSA,L,NC]
RewriteRule ^frontend/images/(.*)$      frontend/images/$1   [QSA,L,NC]
RewriteRule ^(.*)$      frontend/web/index.php/$1   [QSA,L,NC] 

#RewriteCond %{REQUEST_URI} ^frontend/(assets|css|js|images|themes)
#RewriteRule ^frontend/images/(.*)$      frontend/images/$1   [QSA,L,NC]

#RewriteCond %{REQUEST_URI} ^/frontend/web/projects/$
#RewriteRule frontend/projects/(.*)$ frontend/web/index.php/projects/$ [L]
#RewriteCond %{REQUEST_URI} ^(?!.*/themes/)

#RewriteCond %{REQUEST_URI} ^/(assets|js|images|themes|uploads)
#RewriteRule ^themes/(.*)$ backend/web/themes/$1 [L]
#RewriteRule ^images/(.*)$ backend/images/$1
#RewriteRule ^css/(.*)$ backend/web/css/$1 [L]



#RewriteRule ^(.*)?$ backend/themes?$ [L,PT]
#RewriteRule ^(.+)?$ frontend/web/$1
#
#RewriteCond %{REQUEST_URI} !^/backend/(assets|css|themes)/
#RewriteRule .* backend/themes/$1 [L]  

#Options -Indexes
#RewriteEngine on


#
#RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css)/
#RewriteCond %{REQUEST_URI} !index.php
#RewriteRule frontend/web(.*) frontend/web/index.php [L]

#RewriteCond %{REQUEST_URI} ^/(assets|css|js|images|themes)
#RewriteRule ^assets/(.*)$ backend/web/assets/$1 [L]
#RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
#RewriteRule ^js/(.*)$ frontend/web/js/$1 [L]
#RewriteRule ^images/(.*)$ frontend/web/images/$1 [L]
#RewriteRule ^themes/(.*)$ frontend/web/themes/$1 [L] 

Below is the .htaccess from /frontend/web/

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

Please help. I am unable to resolve this. I am actually not able to understand the cause.

Upvotes: 0

Views: 1334

Answers (2)

akshaypjoshi
akshaypjoshi

Reputation: 1295

You need to create two .htaccess. One in your project root folder and another in your frontend/web folder.

Please follow the steps of Anant Singh's answer of this question.

Yii2:-Pretty URL's are formed, but not working (says 404 “NOT FOUND”)

I hope It will help.

Upvotes: 1

Kandarp Patel
Kandarp Patel

Reputation: 1020

RewriteRule ^(.*)$      frontend/web/index.php/$1   [QSA,L,NC] 

Change above rule in your root .htaccess to

RewriteRule ^(.*)$      frontend/web/$1   [L,NC] 

Upvotes: 0

Related Questions