How to remove index.php from URL in Yii2

来源:互联网 时间:1970-01-01

To remove the index.php, create a file called htaccess at the root/web/ of your YII2 site. You can find out more about .htaccess here, but for our purposes here we just want to use it to hide index.php in our URLs. Not every web server is the same, but something along these lines should work for your purposes. Make sure you have mod_rewrite enabled on your server.

.htaccess file :

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

Now go to config/web.phpadd edit urlManagerconfigurations under components section.

'urlManager' = > [

'class' = > 'yii/web/UrlManager' ,

'showScriptName' = > false ,

'enablePrettyUrl' = > true ,

'rules' = > array (

'<controller:/w+>/<id:/d+>' = > '<controller>/view' ,

'<controller:/w+>/<action:/w+>/<id:/d+>' = > '<controller>/<action>' ,

'<controller:/w+>/<action:/w+>' = > '<controller>/<action>' ,

) ,

] ,

Now browse your application and you will found clean URL and index.php has been removed from URL in your Yii2 application.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.