Yii2 official documentation has a page about Shared Hosting configuration that provides an example of how you can configure Apache for SEO friendly URLs using an .htaccess file.
http://localhost/youapp/site/about/
Options +FollowSymLinks
IndexIgnore */*
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
After that you need to configure urlManager in your
config/web.php file.$config = [
//...
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
],
//...
If your application is located in the root folder the routes will look something like this: http://yourdomain.com/web/site/index,otherwise if your Yii2 app is in a subfolder they might look like this http://yourdomain.com/app/web/site/index
Hide the web folderYou can get rid of the web part of the URL by moving all the files from the web folder to your applications root folder and changing the paths in index.php, but this method will mix the contents of the/assets/folder and the/web/assets/folder. This is why this solution is better suited for the advanced application template, unless you are willing to move all of your code along with the vendors folder into a separate folder like Yii 1.x did.A better way to solve this is by adding an additional .htaccess file to the root of your app.Options -Indexes RewriteEngine on RewriteRule ^(.*)$ web/$1 [L]Now we have to tell Yii that our base URL has changed. We do this by configuring baseUrl for urlManager and the request component inconfig/web.php.$config = [ //... 'components' => [ 'urlManager' => [ 'class' => 'yii\web\UrlManager', 'showScriptName' => false, 'enablePrettyUrl' => true, 'baseUrl' => '/', ], 'request' => [ 'baseUrl' => '', //... ], //...If your app is in a subfolder put the name of the subfolder into the baseUrl parameter ‘baseUrl’ => ‘/youapp’,.
Hiç yorum yok:
Yorum Gönder