Laravel - How to remove index.php from Laravel in windows xampp - php

Problem:
my routes not working except the root home page, I'm searching for two days to find a solution to this problem and what I found that I should change .htaccess file but solutions didn't fix any for my case, at first the url localhost/quotes/public was working well with me, but at some point I'm not sure what is it this issue showed up
what I tried:
create another route and I made sure that no routes are working only
home route, still not working except home
tried to change OverrideMode on my XAMP from None to All, didn't fix any
tried to type manually localhost/quotes/public/index.php BOOM everything
works ..
my htaccess file:
<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]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
working on:
Windows 10
XAMP
Laravel 5.2.35

The problem is that your .htaccess is rewriting everything to the frontcontroller, which is normally located at {host}/index.php. In your application however it is located at {host}/quotes/public/index.php.
So you have 2 options:
1. virtual host
Set up a virtual host in your XAMPP Apache that points ie. myapp.local to htdocs/quotes/public Here is an example of how to achieve this: how to create virtual host on XAMPP. (Don't forget to add the host to your hosts file and have it point to your local macine on 127.0.0.1) You can then access your application on myapp.local/whatever-route-you-define. Alternatively you forget about XAMMP and install the homestead virtual machine, which comes preconfigured for this.
2. rewrite rule
Change you rewrite rule to rewrite all requests to quotes/public/index.php in stead of index.php. I'm no htaccess expert, but I believe it should be as simple as changing this:
RewriteRule ^ index.php [L]
to this:
RewriteRule ^ quotes/public/index.php [L]
Do note that you'll still need to access your application trough localhost/quotes/public/whatever-route-you-define which is not ideal imo. Your dev version should be as close to your live version as possible, and if you start working with absolute and relative paths and stuff in your code things will become a mess sooner rather then later.
Personally I would go for Homestead, I use it all the time and it works great once you have it running.
Btw, the reason why localhost/quotes/public/index.php is working for you right now is because RewriteCond %{REQUEST_FILENAME} !-f tells Apache not to rewrite any requests to files that actually exist (otherwise you wouldn't be able to access static assets like your css).

The .htaccess file must be at the root of the application.
Add this in this file :
RewriteEngine On
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
Assuming you haven't touched the original architecture of Laravel, and that public data is still in the same place : the public/ folder
You can also follow this good tutorial

Let me give you an example of the way I have my routes setup.
In app\Http\routes.php, here are three sample routes that I have.
Route::get('/', function () {
$values = app('App\Http\Controllers\KeywordController')->index();
dd($values);
return view('welcome');
});
Route::get('googlefile', function () {
$output = app('App\Http\Controllers\KeywordController')->printToFileGoogle();
dd($output);
});
Route::get('bingfile', function () {
$output = app('App\Http\Controllers\KeywordController')->printToFileBing();
dd($output);
});
I have WAMP setup on my environment. I have made a controller at app\Http\Controllers\KeywordController.php. If my browser is set to localhost/googlefile, then it will goto the method printToFileGoogle() in KeywordController.php.
Please try something similar to this and tell me if you get an error and if you do what error you get.

Related

Laravel new project routing not working?

Laravel new project routing not working? This is the routes.php file:
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('ID/{id}',function($id){
echo 'ID: '.$id;
});
Route::get('/user/{name?}',function($name = 'Virat Gandhi'){
echo "Name: ".$name;
});
So what i have done is this. I started the local laravel development server with: php artisan serve. Just like the book told me that i am going through (Laravel 5). But now only the first routing works '/' which uses the welcome view blade template.
But all other routings don't work >.<
Can someone please help me? I'm stuck.
My app/public/htacess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I tried your all routes it's working fine.
Note:routes are case-sensitive make sure you are accessing the same route as you mentioned in the route.php
I tried below routes :
http://localhost:8000/user/1
return :Name: 1
http://localhost:8000/ID/1
return :ID: 1
You have to use return not echo
Like this:
Route::get('ID/{id}', function($id) {
return 'ID: ' . $id;
});
I tried your all routes it's working fine.
Note:routes are case-sensitive make sure you are accessing the same route as you mentioned in the route.php
I tried below routes :
http://localhost:8000/user/1
return :Name: 1
http://localhost:8000/ID/1
return :ID: 1
Note : make sure you run apache & mysql services from xamp before you serve you project.
Sorry i'm not able to tell this in the comments above as i don't have enough rep.
I think the problem is your mixing the servers you are using. First try launching your LAMP server and start apache and mysql. Since you've said that the apache launched by LAMP is pointed at port 80, you can try and access it directly on the browser (e.g. http://localhost/yourproject/public/user/1).
I'm guessing since you're using LAMP, your project is under the www root. That's why i've entered the full path for accessing the user route.

Hosting several websites on wamp with laravel breaks the routes

I'm currently hosting several applications on a quite out-dated wamp version but that's not really the case. Everything is working fine on that part, besides my laravel application.
My .htaccess in the www folder looks like this;
RewriteCond %{HTTP_HOST} ^sapdfr.org$ [NC]
RewriteRule ^((?!sapdfr/websitev3).*)$ /sapdfr/websitev3/public/$1 [NC,L]
The thing that's strange, is that the first page is working but when I'm going deeper into the website, it just totally breaks. I've been trying to rewrite these rules but I'm seriously mindblown about all the possibilities. Any help will be greatly appreciated since I'm entirely lost here.
Maybe a silly question but have you try that ? :)
Laravel includes a public/.htaccess file that is used to provide URLs without the index.php front controller in the path. Before serving Laravel with Apache, be sure to enable the mod_rewrite module so the .htaccess file will be honored by the server.
If the .htaccess file that ships with Laravel does not work with your Apache installation, try this alternative:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
and then just redirect your domain to the laravel public folder

Laravel not recognized after .htaccess changes

Here is the baffling sequence of events that just transpired.
1) I have a working Laravel installation, with an empty .htaccess file, all URLs work.
2) I add some rules to .htaccess just to play around a bit, then remove the rules.
3) Now I have an empty .htaccess again (so nothing has changed compared to step 1 above) but now no URLs work anymore except the homepage.
For example, you will see that http://jovansprojects.com/march_madness/ works (that's the root of the Laravel folder), using this route:
Route::get('/', 'HomeController#showWelcome');
But http://jovansprojects.com/march_madness/login does not work anymore, despite having worked normally before.
This is the relevant route:
Route::get('login', 'SessionsController#create');
Note that it doesn't throw a Laravel error, like a 'not found exception' - it simply doesn't seem to know Laravel is installed there at all.
I've tried clearing Artisan's cache according to some online suggestions but didn't help.
So, to sum up: .htaccess is exactly the same as it was before (empty) but it seems the server doesn't even know to ask Laravel to handle the URLs anymore, not beyond the homepage anyway.
These are the rules I added to, and then removed from, .htaccess (I wanted to redirect trailing slashes):
Options -MultiViews
RewriteEngine On
RewriteBase /march_madness/
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ $1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
I suspect that RewriteBase may have something to do with it, but after I deleted the rules why didn't it revert to the way it was?
It would seem that Laravel does need the "Front Controller" in .htaccess in order to be able to route "pretty" URLs (ie. without having to use index.php in the URL and rely on PATH_INFO).
So, at the minimum, something like the following would be required in the .htaccess file (probably in the root of your Laravel install):
RewriteEngine On
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
It would seem that there were two .htaccess. Note that .htaccess files are inherited along the filesystem path. However, mod_rewrite (to which the directives above belong) is not inherited by default. So, if you enable the rewrite engine in a subdirectory then the mod_rewrite directives in the parent directory will be ignored.

access a folder in 'public' directory of laravel

How can I access a folder placed in the public directory of my default laravel installation. This question is for both - testing as well as live purpose.
While Testing-
I am testing on windows using XAMPP's apache http server. I have placed my app's root folder in the htdocs folder of XAMPP. Thus, when I try to access a folder in public directory, 'localhost/myappname/public/blog' it redirects me to 'localhost/blog'.
I don't want this behavior. I want it to pick up the 'index.php' file present at 'localhost/myappname/public/blog/' location. But it doesn't do so.
While Live-
I have my site live on ubuntu with apache http server. When I try to access 'mysitename.com/blog' in the firefox, it gives me this error -
Firefox has detected that the server is redirecting the request for this
address in a way that will never complete.
I have no hint about what's wrong. I have tried spending two days working with laravel's default .htaccess file(in public folder) and apache httpd.conf but still not able to resolve it. Have searched a lot over stackoverflow and google but still no clue about where I am wrong. Please guide me.
Added:
The content of .htaccess file is -
<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]
You just need to comment this line in your .htaccess file.
RewriteRule ^(.*)/$ /$1 [L,R=301]
Comment it or strip out from the file. This will solve both your directory /blog issue as well as redirect loop issue.
I used the Alias feature of apache to overcome this problem. I added an alias for '/blog' in my virtual host configuration.

Strange situation mod_rewrite and PHP

I do not understand something ...
I have CMS written by me and have some strange problem about hidden error from "mod_rewrite". In apache2 error.log "mod_rewrite" giving :
File does not exist: /home/path/to/request, referer: http://IP/request?view=1
my $_GET / $_POST request are all empty
$_SERVER['REQUEST_URI'] is ok (returns request?view=1)
$_SERVER['QUERY_STRING'] is also empty
also i install Drupal CMS to test is it problem on web server, but Drupal works just fine... then i copied drupal ".htaccess" file to my CMS in order to fix mod_rewrite preferences. (Drupal do not create apache error like my )
No luck, i have same problem again.
also tried with all options in .htaccess from Drupal CMS
I try to turn on RewriteLog (several attempts) but without result, not a single log file was not created.
Am i something missing or ... just my cms sucks
and one more thing, my CMS work just fine on other webserver with exactly the same files .... ?????
RewriteEngine On
Options -Indexes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
#tested RewriteRule ^ index.php [L]
#tested RewriteRule ^ index.php [L,QSA]
#tested Options FollowSymLinks
#tested Options -MultiViews
#tested RewriteBase /
P.S. sorry for my bad English
Not sure if that's your whole .htaccess but you need to enable rewrite, here's a rewrite I use and its never failed:
RewriteEngine On
Options -Indexes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
The RewriteRule passes everything (.*) to a route parameter in index.php then my router class handles the controller ect, the QSA flag will also allow me to pass extra GET/POST values to any part of the script.
hope it helps
ps I dont add the RewriteCond %{REQUEST_FILENAME} !-d because I dont allow folder views and I dont serve content from a folder, everything goes through a controller inc (images,css,js), so there is no need for this in most cases. And it also protects the folders like core & template ect ;)
finally I found the solution of my problem!
The answer is very,very,very simple...
In apache2 configuration (on Ubuntu) I found the <Directory tag is duplicated by default ... I just erase all directories tags and create one basic.
reload apache2 configuration and every thing starts working just fine

Categories