1

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
tereško
  • 58,060
  • 25
  • 98
  • 150
Poula Adel
  • 609
  • 1
  • 10
  • 33
  • Have you considered trying to fix the problem that is causing your non-root routes from not working? Can you show us the code in your routes.php file? – Kenny Grage Jun 07 '16 at 18:12
  • did you mean `route.php` ? my routes are workig correctley wen I use `localhost/quotes/public/index.php` and I checked it over and over, Nothing wrong I just following a tutorial ! – Poula Adel Jun 07 '16 at 18:15
  • usually the routes are put into `app/Http/routes.php`. – Kenny Grage Jun 07 '16 at 18:27
  • Oh, this one.. yea I checked too.. every thing is alright.. just a question .. can you chat with me ? I'll wait or you Skype: Poula_a.fouad – Poula Adel Jun 07 '16 at 18:31
  • Unfortunately I am not able to right now. Please refer to the answer I just made with example routes and if you are having problems, please feel free to write back with a question. – Kenny Grage Jun 07 '16 at 18:33
  • okey I just wanted to ask about your experience about freelancing and how to make money from coding – Poula Adel Jun 07 '16 at 18:39

3 Answers3

2

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).

Community
  • 1
  • 1
Pevara
  • 14,242
  • 1
  • 34
  • 47
0

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

w3spi
  • 4,380
  • 9
  • 47
  • 80
  • I've did this but unfortunately problem not fixed, also home page gave me `access forbidden` from XAMP .. and yes I'm using default architecture – Poula Adel Jun 07 '16 at 18:06
  • @PoulaAdel And try this : remove all the text inside `.htaccess` and put my solution alone in it. – w3spi Jun 07 '16 at 18:09
  • nope .. still same issue, can you explain to me what these lines do within `.htaccess` ? – Poula Adel Jun 07 '16 at 18:12
  • Theses lines allow you to show the `public/` folder at the root of your application – w3spi Jun 07 '16 at 18:40
0

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.

Kenny Grage
  • 1,124
  • 9
  • 16