I have started learning yii2 and I tried to do pretty URL stuff, but failed. What I did:-
in config/web.php (I have edited below):
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Hide index.php
'showScriptName' => false,
// Use pretty URLs
'enablePrettyUrl' => true,
'rules' => [
],
then I have created a .htaccess
file and put it on root (it has below code):
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
Also I had opened apache2.conf file and changed like this:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All <! -- instead of none -->
Require all granted
</Directory>
Also I checked the changes through the command:
grep -R AllowOverride /etc/apache2
And it shows like below:
/etc/apache2/apache2.conf: AllowOverride All <!-- It is showing that done -->
Now:
when I access my page through:
it's opened and when I hover on About link it shows me: http://localhost/yii2/web/site/about (which shows that pretty urls maid)
But this url is not working (says 404 found)
I have tried below posts each code:
Why dont you just give the rules in your web.php file? like below:
The rules i set here is only an example, you can set it the way you want your url to look like.
EDIT: If its not still working, try to set a virtualhost instead with:
Finally I made it working:-
1. created two .htaccess file (one on root and one in web folder of my application):-
root .htaccess:-
web folder .htaccess:-
2.In config/web.php:-
3. Changed in (apache2.conf) like below:-
4.Then run below commands:-
a.
sudo /etc/init.d/apache2 stop
(to stop apache)b.
sudo killall apache2
(to kill process and to check that no process is running)c.
sudo netstat -l|grep www
(to check port 80 is not in use right now)d.
sudo /etc/init.d/apache2 restart
(restart apache)And now everything worked fine.
By the Way Thanks to every-one who tried to help me out.
Reference taken:-
https://www.my-yii.com/forum/topic/how-to-set-up-pretty-urls-in-yii2-basic-template
https://askubuntu.com/questions/48362/how-to-enable-mod-rewrite-in-apache