Laravel rewrite not working - php

I'm trying to make htaccess rewrite work on apache2 ubuntu 17.04 however it won't work. what i'm trying to do is access the route with localhost/anyroute but the only way to access it is localhost/PROJECTNAME/public/anyroute. localhost/ANYROUTE works on windows but apache will return "The requested url not found" on ubuntu and i have no idea why.. here are my confs file
.htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
my vhost
<VirtualHost *:80>
DocumentRoot /var/www/html/test/public/
<Directory /var/www/html/test/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/test-error_log
CustomLog /var/log/apache2/test-access_log common
</VirtualHost>
apache2.conf
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
so bottom line is that localhost/anyroute won't work but localhost/project/public/anyroute will work. i have pretty much tried everything :/

Enabling mod_rewrite
Now, we need to activate mod_rewrite.
sudo a2enmod rewrite
This will activate the module or alert you that the module is already in effect. To put these changes into effect, restart Apache.
sudo service apache2 restart
We will need to set up and secure a few more settings before we can begin.
First, allow changes in the .htaccess file. Open the default Apache configuration file using nano or your favorite text editor.
sudo nano /etc/apache2/sites-enabled/000-default.conf
Inside that file, you will find the block on line 1. Inside of that block, add the following block:
/etc/apache2/sites-available/default
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Your file should now match the following. Make sure that all blocks are properly indented.
/etc/apache2/sites-available/default
<VirtualHost *:80>
<Directory /var/www/html>
. . .
</Directory>
. . .
</VirtualHost>
To put these changes into effect, restart Apache.
sudo service apache2 restart
Now, create the .htaccess file.
For Detail Follow Below Reference Link :
https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite-for-apache-on-ubuntu-14-04

Related

Apache mod_rewrite and php

I have a slight of problem.
I used simple php framework on github and have worked on it and added new functions and so on.
The framework that i have had help with is this:
https://github.com/panique/mini
Everything works perfectly in a MAMP environment, but now when i was going to test it on a live website and apache environment it does not work that good.
It obviously can read the index file since i point the directory to the directory that should be used for apache.
But when i am going to make a call to a controller (domain.com/login) it results in apache giving me :
"Not Found: The requested URL /login was not found on this server."
I have enabled rewrite with: service a2enmod rewrite, and i double checked and it is a Loaded module when looking in phpinfo().
The .htaccess file that is located in the "/var/www/html/php-project/public" directory(where i have pointed to in my virtual host file"
is the following:
# Necessary to prevent problems when using a controller named "index" and having a root index.php
Options -MultiViews
# Activates URL rewriting
RewriteEngine On
# Prevent people from looking directly into folders
Options -Indexes
# If the following conditions are true, then rewrite the URL:
# If the requested filename is not a directory,
RewriteCond %{REQUEST_FILENAME} !-d
# and if the requested filename is not a regular file that exists,
RewriteCond %{REQUEST_FILENAME} !-f
# and if the requested filename is not a symbolic link,
RewriteCond %{REQUEST_FILENAME} !-l
# then rewrite the URL in the following way:
# Take the whole request filename and provide it as the value of a
# "url" query parameter to index.php. Append any query string from
# the original URL as further query parameters (QSA), and stop
# processing this .htaccess file (L).
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
00-default.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName www.domain.com
ServerAlias domain.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/php-project/public
<Directory />
AllowOverride All
</Directory>
#DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
apache.conf
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
#<Directory /srv/>
# Options Indexes FollowSymLinks
# AllowOverride None
# Require all granted
#</Directory>
AccessFileName .htaccess
I dont think the problem is within the php itself.
Thats why i don't added my code since its so many classes, but the code is as i said based on the framework in the beginning of the question.
Do any of you see any obvious problems in the conf's?
I have searched and searched for answers, but i don't seem to get it right. So i would really really appreciate some help.
Any questions, just ask on.
Thanks
Please follow the proper way of access rewrite module in apache2.
First enable the rewrite module in apache using below command-:
$ sudo a2enmod rewrite
Open the apache2.conf file
$ nano /etc/apache2/apache2.conf
Update the section
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Remove the section from 00-default.conf
Remove the below lines
<Directory />
AllowOverride All
</Directory>
Then finally restart your apache2.
$ sudo service apache2 restart
or
$ sudo /etc/init.d/apache2 restart

How to enable .htaccess in amazon ec2 running ubuntu

I have an instance running ubuntu in ec2. I have this .htaccess file :-
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This file is located inside the var/www/html folder. I am aiming to achieve loading of amazon-public-dns.com/index.php as amazon-public-dns.com/index. Now, I have tried these steps :-
1)
Creating the rewrite.conf in /etc/apache2/mods-enabled and in the file putting the line LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so. (Refer this answer)
2)
Running the command apache2 enable module rewrite. Also, in the file /etc/apache2/sites-available/000-default.conf and writing this in the end :-
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
# changed from None to FileInfo
AllowOverride All
Order allow,deny
allow from all
</Directory>
Note that this answer told to edit the default. But, since that file was not there in my case, I edited the 000-default.conf file.
I restarted apache as told. But the link amazon-public-dns.com/index gives me a 404 :( Please help me.
EDIT : I put some random junk in the htaccess file. But the index.php is not giving any 500 internal server error which means that the htaccess file is being ignored. Now in
https://help.ubuntu.com/community/EnablingUseOfApacheHtaccessFiles
, they say to enable htaccess edit this file - /etc/apache2/sites-available/default. BUT THERE IS NO SUCH FILE
following command works for me Great !
sudo a2enmod rewrite
sudo service apache2 restart
To check loaded modules
sudo apache2ctl -M
The default conf file /etc/apache2/sites-available/000-default.conf only work if its having a symlink in sites-enabled folder.
You have done all right steps. Its may be problem of sysmlink of sites-available folder. You can either create a symlink or update 000-default.conf with AllowOverride None to AllowOverride All inside file /etc/apache2/sites-enabled/000-default.conf
Today I faced this exact same problem on an ubuntu EC2 instance.
This worked for me:
Place your .htaccess configuration inside the <IfModule ..> directive:
<IfModule mod_rewrite.c>
RewriteEngine on
# your config here
</IfModule>
Not sure if /var/www/html is actually taken into account by the server.
Try changing this:
<Directory /var/www/html>
# your current implementation
AllowOverride All
</Directory>
for this one:
<Directory /var/www>
# your current implementation
AllowOverride All
</Directory>
This directive should already by there in your /etc/apache2/apache2.conf file.
If that does not work try to comment out your configuration, particularly the part that has the allow,deny and allow from all
<Directory /var/www>
# your current implementation
AllowOverride All
# Order allow,deny # try to comment this one out
# allow from all # also this one
</Directory>
Last but not least, check that the rewrite a2enmod module is loaded as suggested by #kamal-kumar and do not forget to restart you apache.
This worked for me, hope this helps someone else :)

How to fix permalinks not working with Vagrant and Wordpress?

I am using Vagrant to build up a little Wordpress development VM. When I select permalinks (postname) then the page from an article doesn't load. However, when I select the standard link (i.e page id) all is working good.
I've used the service PuPHPet to build the VM.
My settings can be found here
I am using Wordpress 3.9.1 and Apache2
I've enabled mod_rewrite be executing:
a2enmod rewrite
And my .htaccess file from Wordpress is as follows:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /svisa/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /svisa/index.php [L]
</IfModule>
# END WordPress
it has the following permissions and ownership:
-rw-rw-rw- 1 vagrant www-data 248 May 30 14:52 .htaccess
My virtualhost file for the site (/var/www/svisa/) can be found here.
from my host computer, I browse to the site via adress: http://wpdev-vm/svisa/
where wpdev-vm is the name of the vm.
Does anybody know what I am missing to make the permalinks work?
I solved the problem myself.
In the default apache configuration, under /etc/apache2/sites-enabled, where mine is called 15-default.conf
This was declared under the document root:
<Directory "/var/www">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
I had to change AllowOverride None to AllowOverride All. Thus you'll get the following:
<Directory "/var/www">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
After that, the permalinks started working.
The (correct) answer above says that you need to change AllowOverride None to AllowOverride All in your xxx-default.conf file found in the /etc/apache2/sites-enabled directory.
However, in my vagrant box (precise64) none of the allowOverride or <Directory /var/www/>... code was present to begin with in the default.conf file.
I ended up having to add all of it between the VirtualHost tags, like below:
<VirtualHost *:80>
# Other stuff
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
And then having to restart apache, like below, for it to work.
sudo service apache2 restart

only default permalinks working wordpress others become 404 error

My problem is in my wordpress site only default permalinks is working.. When I change permalinks all pages become not found.. only home page is showing.
When I change permalinks to postname .htaccess content change to
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And for default it is
# BEGIN WordPress
# END WordPress
Also tried to get the status of mod_rewrite using this
in_array( 'mod_rewrite', apache_get_modules())
It returns 1 and mod_rewrite is enabled on my server and also checked using phpinfo(). mod_rewrite module is loaded.. I have gone through all the documents available in internet.. Please help me to solve this problem.. My hosting server is godaddy..
In case you are on Ubuntu, edit the file /etc/apache2/apache2.conf (here we have an example of /var/www):
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
and change it to:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
also check Apache configuration file for your website /etc/apache2/sites-available/your_site.conf:
<Directory /var/www/your_site_path/>
AllowOverride None
</Directory>
and change it to:
<Directory /var/www/your_site_path/>
AllowOverride All
</Directory>
You need to do sudo a2enmod rewrite to enable module rewrite
then,
sudo service apache2 restart
I hope this helps you!
It could be a broken rewrite_rules field in your database.
I installed "Yoast SEO" into a live site and that broke it completely. I disabled the plugin but it didn't go back to working. Only default permalinks would work.
Like everyone else I tried the usual things - checking .htaccess, making sure that mod_rewrite was working on the server, saving the permalinks settings again, but nothing was working. The homepage would load, and wp-admin pages would load, but everything else was just loading the "Latest Posts" default page.
In the end it turned out to be something wrong with the rewrite_rules field of wp_options in the database.
I had three versions of the site (local, staging and production), and all were from one SVN base, so I knew it was not a file issue. But only the local development version was working.
I replaced the contents of the rewrite_rules field of the broken live site with the working one from my local development site, and it worked.
What's really weird is that deleting the rewrite_rules field and having WordPress rebuild it did not fix the problem. Only replacing with the contents of a previously working version of the site did the trick.
I'm still puzzled by this, and will edit this reply if I find out anything further about the problem.
I had the exact same issue and fixed it by running
sudo a2enmod rewrite
Then restarted apache. Apparently the issue is caused by mod_rewrite potentially not working properly on ubuntu.
I found the solution here
suffered from the same issue. Having site on SSL had to change NOT the 000-default.conf but the respective SSL conf file. Adding
after DocumentRoot /var/www/html
the following
<Directory "/var/www/html">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
and then restarting apache2 saved my site.... nothing else worked. The issue was Wordpress SSL and SEO plugins messing up config files.
I had this issue in Wordpress installed on CentOS7 and the solution was to edit httpd.conf file with this command:
sudo vi /etc/httpd/conf/httpd.conf
And replace
<Directory "/var/www/wordpress">
Options Indexes FollowSymLinks
AllowOverride none
Require all granted
</Directory>
with
<Directory "/var/www/wordpress">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
then restart Apache:
sudo systemctl restart httpd.service
Not sure this is too late. i faced the same issue and solved by changing
AllowOverride None
to
AllowOverride All
in /etc/apache2/sites-enabled/000-deafult.conf file. hope this would help.
I faced the same challenge after setting up WordPress on Ubuntu 22 and configuring an SSL certificate for the site.
This is how I was able to resolve it.
Open the apache config file for editing (using nano in this case)
sudo nano /etc/apache2/apache2.conf
Change the following line:
<Directory "/var/www">
Options Indexes FollowSymLinks
AllowOverride none
Require all granted
</Directory>
to:
<Directory "/var/www">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Open the default SSL config file for editing
sudo nano /etc/apache2/sites-available/default-ssl.conf
Add the following code:
<Directory "/var/www/html">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Restart Apache
sudo service apache2 restart
You should now be able to change the site permalink structure without seeing the 404 page not found error.

Enabling pretty permalinks on Wordpress - apache configuration does not work

Probably this error has a pretty easy solution but I've been looking way to long at this and still don't get the error. I think I've tried whatever I could.
Problem: when I enable pretty permalinks on my wordpress installation (so, that it is using /%postname%/), it doesn't work. I get a 404 on all pages except for the homepage.
This page http://codex.wordpress.org/Permalinks tells me the requirements for permalinks to work:
Apache web server with the mod_rewrite module installed
In WordPress's home directory,
The FollowSymLinks option enabled
FileInfo directives allowed (e.g. AllowOverride FileInfo or AllowOverride All)
An .htaccess file (if this file is missing, WordPress will try to create it when you activate "pretty" permalinks)
If you want WordPress to update the .htaccess file automatically, WordPress will need write access to the file.
Apache web server has been installed, the mod_rewrite module has been loaded with a2enmod rewrite command (and the server has been restarted multiple times after). So, under /etc/apache2/mods-enabled, the symlink to rewrite.load is present. Also, when I run phpinfo command, I see that the mod_rewrite module has been loaded. You can check this as well here: http://namorti.com/phpinfo.php
Then, in /etc/apache2/sites-enabled, there was no "default" present. I copied 000-default.conf to default and edited default afterwards. It contains the following:
DocumentRoot /var/www
<Directory />
Options FollowSymLinks Indexes
AllowOverride FileInfo
</Directory>
So as far as I'm concerned, FollowSymLinks has been enabled and FileInfo directives are allowed.
As for the last two points, in my wordpress home directory (/var/www), .htaccess is present and writeable by Wordpress (I updated the permalink structure a couple of times and it updates the .htaccess file accordingly). Right now it contains the following:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
So, as far as I know, it SHOULD be working. I restarted the server (service apache2 restart) several times. I don't see what I'm missing. Anyone has a clue here?
Thanks in advance!
* EDIT *
So, I did what calcinai told me to do... I edited my /etc/apache2/sites-enabled/default file (containing the vhost). It now looks like this:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin www.namorti.com
DocumentRoot /var/www
<Directory /var/www>
Options MultiViews
AllowOverride None
Order allow,deny
allow from all
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
I've restarted apache again, but unfortunately it still doesn't work. Honestly it would have surprised me, because moving the directives from the .htaccess file to the vhost would work if the htaccess on itself would work, as everything else seemed correct enough to me...
Any other suggestions? Thanks for the effort!
Make sure the AllowOverrides directive is set to all in the vhost - that's what tells apache to look for .htaccess files in the webroot.
A better solution is actually to put your rewrite directives in the vhost, as you clearly have write access to the file. When you have AllowOverrides on, apache will look in the directory and all parent directories for .htaccess files on every request, which can be a big performance hit.
For Wordpress, your vhost should look something like:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/site
<Directory /var/www/site>
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
allow from all
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</Directory>
</VirtualHost>
Solved it myself.
It had to do with the file "000-default.conf" - I copied it to "default" and edited "default", like I already mentioned in my question.
Apparently the service is using the file "000-default.conf" on itself. By copying the "default" file back to "000-default.conf" and restarting the apache service, everything works now.
One caveat: calcinai's suggestion seems like it should work, but with his suggestion in my vhost in the correct "000-default.conf" file, I got a 403 Forbidden error. When I replaced his content with my original content
DocumentRoot /var/www
<Directory />
Options FollowSymLinks Indexes
AllowOverride FileInfo
</Directory>
And then once again restarting the apache service, it all worked.
Thanks calcinai for your effort to try to help me :)
Permalink Problem
Inside of this file, we want to change all things.
sudo nano /etc/apache2/sites-available/000-default.conf
000-default.conf
- should look something like this:
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
If you dont see like this then paste the above code inside virtual host
When you are finished, save and close the file.
Next, we need to enable the rewrite module, which allows you to modify URLs. You can do this by typing:
sudo a2enmod rewrite
After you have made these changes, restart Apache:
sudo service apache2 restart
This may help you solve this:
sudo chown -R www-data:www-data /var/www
After spending really lots of time, here is what worked successfully in my Linux machine's LAMP server. Following changes are needed for me to make it work perfectly,
Add this to your /etc/hosts file.
127.0.0.1 sitename.com
Add host entry as follows in the path /etc/apache2/sites-available/sitename.com.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName sitename.com
DocumentRoot /var/www/sitename.com/public_html
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/sitename.com/public_html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Create symlink to sites-enabled.
sudo ln -s /etc/apache2/sites-available/sitename.com.conf /etc/apache2/sites-enabled/sitename.com.conf
Restart apache by using the command,
sudo service apache2 restart
This works perfectly for me. Instructions for adding host entry taken from, https://www.digitalocean.com/community/questions/wordpress-permalinks-not-working-on-ubuntu-14-04

Categories