how to remove url (/web/index.php) yii 2 and set route with parameter with clean url?
Asked Answered
I

4

5

first question: i already remove index.php, but i want remove /web also. this is my .htaccess

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

and this is config/web.php

'urlManager' => [
            'class' => 'yii\web\UrlManager',
            // Disable index.php
            'showScriptName' => false,
            // Disable r= routes
            'enablePrettyUrl' => true,
            'rules' => array(
                    '<controller:\w+>/<id:\d+>' => '<controller>/view',
                    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            ),
        ],

it's working fine, but it's still using /web . is it possible remove /web ?

second question:

i can't set route with parameter with that clean url, my route Url::toRoute(['transaction/getrequestdetail/', 'id' => 1 ]);

how the route should be ? and how with 2 parameter route ?

Insalubrious answered 27/4, 2015 at 1:58 Comment(0)
R
20

For advanced application follow these steps:

1) Add the following htaccess to frontend/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

2) Add the following htaccess to root folder where application is installed

# prevent directory listings
Options -Indexes
IndexIgnore */*

# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteRule ^admin(/.+)?$ backend/web/$1 [L,PT]
RewriteRule ^(.+)?$ frontend/web/$1

3) Edit your frontend/config/main.php file with the following at the top

use \yii\web\Request;
$baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());

4) Add the request component to the components array in the same file i.e frontend/config/main.php

'components' => [
        'request' => [
            'baseUrl' => $baseUrl,
        ],
],

That's it.Now you can access the frontend without web/index.php

For you second question you need to write the rule for it in your URL manager component.

Something like this:

'urlManager' => [
            'baseUrl' => $baseUrl,
            'class' => 'yii\web\UrlManager',
            // Disable index.php
            'showScriptName' => false,
            // Disable r= routes
            'enablePrettyUrl' => true,
            'rules' => array(
                    'transaction/getrequestdetail/<id>' => 'transaction/getrequestdetail',
           ),
],
Repulsive answered 27/4, 2015 at 6:7 Comment(6)
thank's for answer bro, but i still have trouble with second question. the url result still: localhost/transmansys/objecttransaction/index&id=100049 with that rules. i need url result be: localhost/transmansys/objecttransaction/index/100049. and how about url with 2 or more parameter ?Insalubrious
solved bro, i just need add " / " after <id> in your rules lol. thank you very much dude :DInsalubrious
@Chinmay what if I have a basic template. how I would write the number 2? I hope you might answer. thanksHarelda
@Harelda I have not used basic template as yet. But is shouldn't be that hard. This link might help you: yiiframework.com/forum/index.php/topic/…Repulsive
It s working perfectly, even for basic template.ThanksAppraise
This may break some URLs, see explanation and alternative in my answerTrinatrinal
W
4

If it still isn't working after going through the answers above, then you can edit your 'apache2.conf' file in your favorite editor to change

AllowOveride None to AllowOveride All

On Ubuntu, the file is located at /etc/apache2/apache2.conf

The final edit should look like

<Directory /var/www/>
  Options Indexes FollowSymLinks
  AllowOverride All
  Require all granted
</Directory>

Finally, restart the Apache server

Wilds answered 21/10, 2016 at 0:14 Comment(1)
This is the solutionSubjectivism
P
1

You can add the information in file configuration to remove /web:

$baseUrl = str_replace('/web', '', (new Request)->getBaseUrl());

return [
    ...
    'components' => [
            'request' => [
                'baseUrl' => $baseUrl,
     ],
      ...
    ]
]
Panjandrum answered 27/4, 2015 at 2:44 Comment(3)
You can change htaccess: Options -Indexes <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_URI} !^web RewriteRule ^(.*)$ web/$1 [L] </IfModule> # Deny accessing below extensions <Files ~ "(.json|.lock|.git)"> Order allow,deny Deny from all </Files> # Deny accessing dot files RewriteRule (^\.|/\.) - [F]Panjandrum
still "object not found", can you write .htaccess in your answer ? (edit your answer). i maybe I mistyped.Insalubrious
You can move $baseUrl in config to ... 'urlManager' => [ 'baseUrl' => $baseUrl, 'class' => 'yii\web\UrlManager', ...]Panjandrum
T
0

Using str_replace('/frontend/web', '', (new Request)->getBaseUrl()) for detecting base URL is a bad idea and using str_replace('/web', '', (new Request)->getBaseUrl()) is a terrible idea. str_replace() removes all occurences of requested string, so str_replace('/frontend/web', '', (new Request)->getBaseUrl()) for URL /frontend/web/tools/frontend/webalizer will give you /toolsalizer. Definitely not a thing that you want.

If you want to remove this string only from the begging of URL:

$baseUrl = (new Request())->getBaseUrl();
if ($baseUrl === '/frontend/web') {
    $baseUrl = '';
} elseif (strncmp($baseUrl, '/frontend/web/', 14) === 0) {
    $baseUrl = substr($baseUrl, 13);
}

But the best solution would be avoiding the whole problem in the first place, for example by using symlinks to mimic required directory structure for single webroot.

Trinatrinal answered 21/4, 2018 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.