When I load pages that are not root, the page won't load without index.php before the route. The error I get:
Not Found
The requested URL /login was not found on this server.
Apache/2.4.7 (Ubuntu) Server at mydomain.com Port 80
To start with I have a virtual host file containing:
//also tried adding DirectoryIndex here before <directory>
<directory /var/www/mydomain.com>
DirectoryIndex index.php
AllowOverride ALL
</directory>
and a .htacces in my public with :
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I have another domain with the same .htaccess and appache config on the same server and it works fine. Also I did restart apache. If I use phpinfo on my index.php/route page I see at Loaded Modules:
mod_rewrite mod_setenvif
When running the website localy with xampp everything works fine.
For hours i'm trying now but I can't fix it or find any error (logs are empty) or any solutions that I haven't tried yet.
Edit:
Im using Ubuntu Ubuntu 14.04 x64 on a digital ocean VPS. And I tried turning it on and off again (as suggested). PHP Version 5.5.9-1ubuntu4.9. I followed this tutorial to config everything (except the direcoty part). I changed the location of the apache log and a file error.log is created on the given directory but no errors are in it.
My currently appache config is : (i've triple checked the white parts where the domain name is).
When I run apache2ctl -t D DUMP_VHOSTS I get
this looks fine to me, also tried disabling the default config but it didnt help.
Note: i've replaced my real domain with mydomain.com in reality I use my real domain on these spots.
Thought how do I know for sure that the conf file im editing is the one being used by the domain?
This is the correct Alternative htaccess rule for Laravel 5 suggested to use when the default doesn't work:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The Options +FollowSymLinks I believe plays an important role because the DirectotyIndex acts like a symlink.
Also check /app/config/app.php to make sure you haven't set the Config('app.url') to contain the index.php.
If you have laravel installed in a sub-directory see this:
How can I remove "public/index.php" in the url generated laravel?
Have you tried turning it all off (shutdown), and then turning it on again?
If everything is as you say, it should work. I would try a restart to see if it changes anything.
If it doesn't, please update with what OS you are using.
I ended up deleting the complete conf file and copied it again from the one thats working. I took the default .htaccess from laravel and it worked. Unfortunately I still dont know what was wrong though. The current conf file looks like
<Directory /var/www/mydomain.com>
AllowOverride ALL
DirectoryIndex index.php
</Directory>
My .htaccess in /public
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I had almost exactly the same problem today, which #dasper also helped me out on. I am using digital ocean Ubuntu 14.04 also, and I had to run the command a2enmod rewrite then reload apache2. It seemed that even though rewrite appeared to be on, it actually wasn't.
First, make sure mode_rewrite is enabled. I am sure you have probably done this but I want to make sure nothing is missed.
Next, see what version of apache you are running since the syntax has changed between 2.2 and 2.4. This should not be a big deal and highly doubt this is the culprit but could save you trouble in the future
Next, try putting the rewrite condition inside of the vhost information instead of the htaccess file. This can help a little with performance as well as give you a console warn/err when restarting apache where the htaccess errors could be squelched.
<directory /var/www/mydomain.com>
Options -MultiViews
AllowOverride ALL
RewriteEngine On
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</directory>
Beyond this point I would be at a loss without some more logging or information from the server. You can also add CustomLog into the vhost and report all requests processed by the server.
The problem could be with your virtual host configuration and/or a misplaced .htaccess file.
Make sure your .htaccess file is in the public/ folder and that the DocumentRoot is set to that public/ folder.
And also, the < Directory> directive isn't controlling your virtual host but rather controls the folder itself and how it can be (or can't be) accessed on the server. To change the configuration of the virtual host do so inside the < VirtualHost> directive.
Here is an example of a virtual host configuration:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot "/var/www/mydomain.com/public"
DirectoryIndex index.php
</VirtualHost>
Here is the default Laravel Apache config:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
and here is an alternative:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Try to rename the server.php in the your Laravel root folder to index.php and copy the .htaccess file from /public directory to your Laravel root folder.
Solution found here
I believe your directory path is wrong. You need /public on the end because Laravel treats that directory as the web root. Try this:
<directory /var/www/mydomain.com/public>
DirectoryIndex index.php
AllowOverride ALL
</directory>
But the better solution is to put the contents of the .htaccess file in your virtual host. Like this:
<VirtualHost *:80>
DocumentRoot "/var/www/mydomain.com/public"
ServerName mydomain.com
ServerAlias *.mydomain.com
<Directory "/var/www/mydomain.com/public">
# Ignore the .htaccess file in this directory
AllowOverride None
# Make pretty URLs
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
</Directory>
</VirtualHost>
Also, you should make sure your config file is loaded by running:
apache2ctl -t -D DUMP_VHOSTS
This will print a list of all the loaded vhosts. If yours isn't in that list then make sure it is enabled by running:
a2ensite [name_of_your_config_file]
Then restart apache with:
service apache2 reload
One more note: don't forget to edit /etc/hosts and add 127.0.0.2 yourdomain.com
Related
I've a laravel 5.4 over apache centos server.
the folder structure are like:
/var/www/html <- public folder
/var/www/project <- laravel project folder
/var/www/html contains the "public's laravel folder" content like css/ js/ and index.php etc.
index.php contains references to the project main folder.
the project works but just with ip like:
myip/index.php/login
and not
myip/login
as I would like
I try many way to remove it but without luck, this is my .htaccess file inside /var/www/html:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteRule ^(.*)$ index.php/$1 [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Instead of changing the .htaccess file, you should change the server configuration to serve files from Laravel public folder.
Before editing the config file and to be safe, copy the default config file to have a backup: cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.original
Edit the file /etc/httpd/conf/httpd.conf as the follwoing:
change this line:
DocumentRoot "/var/www/html"
to
DocumentRoot "/var/www/project/public"
Add the follwing lines to allow serving files from Laravel public folder:
<Directory "/var/www/project/public">
AllowOverride All
Require all granted
</Directory>
Run sudo apachectl configtest to make sure you did not make any mistake. You should get Syntax OK.
Restart the apache server to apply the new configuration. sudo systemctl restart httpd
Finally, restore the .htaccess file to the default version that comes with laravel at this link.
It's better to create a virtual host instead of changing the default configuration. However, this will be ok if you have only on website on the server.
You need to go on the laravel documentation deployement/apache and you will see the rewriter rules you need to put them rules in your apache configuration. And all will work
For those who work with laravel 7.x: MAke sure have .htaccess in both public and root folder of your project with bellow content
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
By the way, don't forget to set AllowOverride to All in /etc/httpd/conf/httpd.conf (on CentOS):
<Directory "/var/www/path/to/my/app/public">
Options FollowSymLinks
AllowOverride All
</Directory>
Goodluck.
LARAVEL 5.0 | APACHE 2.4.7 | PHP 5.5.8
I know this is a pretty popular question. A lot of people saying to move public contents to root directory (which i find awkward because of security issues), rewriting the .htaccess, creating virtual host.
I tried creating virtual host because i think this is more acceptable solution.
but i'm encountering some problems with my configurations:
I'm running easyphp for the meantime, id like to test if i can get rid of the public route in url before setting this up live.
httpd.conf
NameVirtualhost *:80
<VirtualHost *:80>
DocumentRoot "${path}/data/localweb/quantum.dev/public"
ServerAdmin admin#localhost
ServerName quantum.dev
ServerAlias www.quantum.dev
<Directory "${path}/data/localweb/quantum.dev/public">
AllowOverride All
Options Indexes FollowSymLinks
Order deny,allow
Allow from quantum.dev
Deny from all
Require all granted
</Directory>
</VirtualHost>
I modified C:\Windows\System32\drivers\etc
hosts
127.0.0.1 quantum.dev
When I try to browse http://127.0.0.1/quantum.dev/ it will load the listof files and not the project inside public
http://127.0.0.1/quantum.dev/
Please revert all your changes to the directory. restore the default structure and configuration of the project. If i'm not mistaken and correct me if i am, you are using a shared hosting provider. If that is the case, here is a more proper way of doing so.
In your .htaccess, you need to add the following.
At the top of the file add this line:
DirectoryIndex public/index.php
and in here:
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Change the last rule to this:
RewriteRule ^ public/index.php [L]
Now, all you need to do is to move this, and only this(the .htaccess file), to the root folder of your project. In this way, you are maintaining the laravel project structure in its original state.
put this .htaccess file near public folder
Options -MultiViews
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
When you are selecting a directory for your domain in c-panel, simply provide path till the Laravel's public directory and not just till the Laravel's root directory. That way you don't need to change .htaccess at all.
I have just recently upload my site to the production and follow above steps. It worked very well.
This is my configuration that is working fine for me. So first of all, your Virtual host configuration should be like that :
ServerAdmin webmaster#domaine.dev
ServerName domaine.dev
DocumentRoot /var/www/mysiteweb/public/
<Directory /var/www/mysiteweb/>
Options -Indexes
DirectoryIndex index.php index.html
AllowOverride All
</Directory>
Anf after, make this on your .htaccess laravel file :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Don't forget to check if the mod_rewrite is enable.
Hope that helped. Regards.
I recently bought a VPS and I'm trying to deploy there a personal laravel project which runs perfectly in localhost.
What I've done is to tar all the project and decompress it on the VPS (/var/www/). I've installed a LAMP, and create the site configuration (/etc/apache2/sites-available/anuncios.com.conf) and enabled it (creating the link on /etc/apache2/sites-available) like so:
<VirtualHost *:80>
ServerName 137.94.162.235
DocumentRoot "/var/www/anuncios.com/public/index.php"
<Directory "/var/www/anuncios.com/public">
AllowOverride all
</Directory>
</VirtualHost>
It's a laravel 5.1.26, with the tipycal .htaccess under the public dir:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
The site works but it's not loading the resources like .js or .css files. Checking the link, for instance:
http://137.94.162.235/css/externos/bootstrap.min.css
I got a Laravel error: 404 Uups, page not found.
Any help for making it work please?
Ownership is 1000:apache2, and permissions are the same that in my localhost (except public that now is 777 just to be sure).
Edit: I followed Huzaib's advise and now it loads everithing, the remaining problem is that now I have to address the site as 137.94.162.235/index.php otherwise 137.94.162.235 it's not working.
In your apache configuration, DocumentRoot has to be a directory not a file.
DocumentRoot "/var/www/anuncios.com/public/"
Make the advised change and then restart the apache2 service.
Solved,
Firstly, I followed Huzaib Shafi answer and changed the /etc/apache2/sites-available/anuncios.com.conf changing the DocumentRoot to the public folder (not the index.php).
Finally, to avoid having to specify the index.php I had to enable the apache mod rewrite from the command line:
a2enmod rewrite
That allows apache to follow some instructions from the public/.htaccess.
I checked many posts here about removing index.php from url but I could not get the desired results, I already added my local url site name to virtual hosts file (I use wamp) and when I write url like this it work http://first-app.com
My problem when I navigate to another folder like "songs" this url does not work, I get url not found
http://first-app.com/songs
while if I used index.php it work
http://first-app.com/index.php/songs
here is my httpd-vhosts.conf file:
<Directory C:\wamp\www\composer-demo\first-app\public>
Order Deny,Allow
Allow from all
</Directory>
<VirtualHost *:80>
DocumentRoot "C:\wamp\www\composer-demo\first-app\public"
ServerName first-app.com
</VirtualHost>
Can that be solved from modifying this file ? or I need to modify .htaccess file located at the public folder ? here it is:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
All I need was to enable rewrite_module in the apache server then restart, since I use wamp I only selected the module from the list menu
Here is a complete list how it can be done for Mac, Linux and Windows
I'm trying to create an app with laravel 4 but i'm facing a url issue. i have wamp installed on my machine. i set up a new virtual host in my httpd-vhost.conf with this code
<VirtualHost mobile.dev>
DocumentRoot "C:\wamp\www\mobile.dev\public"
ServerName mobile.dev
<Directory "C:\wamp\www\mobile.dev">
Options FollowSymLinks Indexes
AllowOverride All
Order deny,allow
Allow from 127.0.0.1
Deny from all
Require all granted
</Directory>
mobile.dev is a folder and also the domain name in my localhost.
here is my Route.php file
Route::get('/','HomeController#showWelcome');
Route::post('login','LoginController#userLogin');
Route::get('login','LoginController#getUsers');
when i ask for mobile.dev/login it gives me the requested url was not found.
can you help me please solve this issue. but when i ask for mobile.dev/ its working.
Here is my .htaccess file :
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Here is more details. when i remove my htaccess content everything works fine with the default url http://mobile.dev/index.php/login
So the problem is in my htaccess and the rewriting in my virtual host
It might be not Laravel problem, but seems your wamp server haven't enabled mod_rewrite in httpd.conf file yet. Locate the line -
#LoadModule rewrite_module modules/mod_rewrite.so
and remove the comment symbol '#'. Then save the file and restart Apache.
Solution:
1- when you have wamp installed in your machine please go to C:\wamp\bin\apache\Apache*.*.*\conf\httpd.conf and make sure that rewrite_mod is enabled by deleting the hash tag. (instead of using the interface provided by wamp)
2-there are two ways to enable rewriting for laravel in .htaccess file :
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
or using this :
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
3- if you're setting a virtual host be sure to add this option in the <virtualhost> tag
<Directory "C:\wamp\www\mobile.dev\public">
Options FollowSymLinks Indexes Multiviews
AllowOverride All
</Directory>
Thanks a lot for your time.
Have you tried
Route::post('login','LoginController#userLogin');
without the () after userLogin?
Your <Directory "C:\wamp\www\mobile.dev"> in config should be <Directory "C:\wamp\www\mobile.dev\public"> to specify the public directory to the root of the htaccess rewrite rules.
I originally thought you were missing the </IfModule> at the end of your .htaccess file but when I posted my .htaccess file it hid the </IfModule> as well so that is a stack overflow issue. Otherwise your htaccess is correct.