简体   繁体   中英

Laravel in subfolder on webhost

I have a webapp written in Laravel that needs to run in a folder on a web host. The app will have to be accessible via hostname.com/webhit/ . This will point to the app's home page.

I only have one route:

Route::controller('/', 'HomeController');

HomeController's getIndex needs to serve the home page. This works.

However, as soon as I want to go to something like hostname.com/webhit/login , I get a 404 from Apache.

Obviously, .htaccess is not working properly. I need it to, essentially, turn URLs that look like hostname.com/webhit/login into hostname.com/webhit/index.php/login .

I have a .htaccess file in www/webhit (where index.php is located) that looks like this:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ webhit/index.php/$1 [L]
</IfModule>

I am not very familiar with .htaccess file syntax, but I believe it's doing something wrong.

Edit:

I did it. My .htaccess was wrong (it actually causes a redirect loop), but the issue was that it wasn't even being parsed by Apache (hence the 404 instead of a 500 due to >10 redirects in a request). I did the following steps in order to get everything to work:

  1. Enable mod_rewrite and restart Apache (plenty of docs out there on how to do this)
  2. But wait, there's more! By default, Apache on Ubuntu prevents URL rewrites. See this site . Most importantly, the following fragment from the URL above is very important: "By default , Ubuntu's Apache will ignore the directives in your .htaccess files." You will need to actually enable rewrites by editing \\etc\\apache2\\sites-available\\default and setting AllowOverride to all (see link above for more details).
  3. Reload the configuration (or just restart apache).
  4. Make sure you're using the correct .htaccess . My original version actually has a redirect loop in it. See the selected response for the correct version.

After this, I got it to work. I hope it helps future programmers having a similar issue!

Check default server requirements - laravel .htaccess file works for most situations. Try with this:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

I would suggest you to use resourceful controllers - mappings from your route to your controllers methods are much more clear, and you'll get full resource with one command (routes,models,views,controllers)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM