7

I am trying to deploy a laravel application on A2Hosting shared hosting. My document root is: /public_html directory. I uploaded everything from my laravel application except the public folder to the /beta directory of hosting.

And then I uploaded everything from the public directory to the /public_html directory.

In my index.php file, I changed the following two lines:

require __DIR__.'/../beta/vendor/autoload.php';
$app = require_once __DIR__.'/../beta/bootstrap/app.php';

Now I am only seeing the home page of my application correctly. That is, mydomain.com. Any hyperlink followed by mydomain.com is showing a 404 message. In my view files, this is how I am referring to a path:

<a href="/login">Login</a>

But after deploying the application, whenever I hit that link, i.e. mydomain.com/login, I get the 404 Not Found: The resource requested could not be found on this server! message. I tried changing /login to login in the <a> tag. Same result. How do I solve this?

core114
  • 5,155
  • 16
  • 92
  • 189
Tanmay
  • 3,009
  • 9
  • 53
  • 83

2 Answers2

18

Eisenheim, this is htaccess issue: get one .htaccess file in root folder of your web-project.

And put the following code inside it,

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

Then try without index.php it should work perfectly.

Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
  • When you say root folder of your web-project, do you mean the public folder of the laravel application which I copied to /public_html folder, or the laravel application root which I copied to the /beta folder? – Tanmay Oct 09 '17 at 13:24
  • laravel folder root. so yes, inside `beta` folder. Where you can see composer.json and composer.lock etc files. – Himanshu Upadhyay Oct 09 '17 at 13:25
  • Added the .htaccess file and tried without index.php, didn't work – Tanmay Oct 09 '17 at 13:30
  • And still with index.php/login, I have the access. If that information helps you... – Tanmay Oct 09 '17 at 13:30
  • Can you check with phpinfo() function if `modrewrite` is enabled and loaded ? – Himanshu Upadhyay Oct 09 '17 at 13:31
  • is it laravel 5.4 ? – Himanshu Upadhyay Oct 09 '17 at 13:31
  • You can follow the steps described in [this link](https://stackoverflow.com/questions/23837933/how-can-i-remove-public-index-php-in-the-url-generated-laravel) for Laravel 5.4 in 1st answer or in 2nd answer its mentioned how to do with laravel 4.2 – Himanshu Upadhyay Oct 09 '17 at 13:33
  • Laravel 5.5. PHP version 7.0.24. There is no such thing as modrewrite in my phpinfo() output. Used ctrl+f and searched for modrewrite – Tanmay Oct 09 '17 at 13:35
  • ok, if it is ubuntu/linux server then, you can execute this command using ssh login via console: `sudo a2enmod rewrite` (to enable modrewrite) and then restart apache using this: `sudo service apache2 restart` – Himanshu Upadhyay Oct 09 '17 at 13:40
  • 1
    It worked when I added the .htaccess file to the /public_html folder. So I guess the htaccess should be added to public folder of the laravel and not the application root. – Tanmay Oct 09 '17 at 13:42
1

An alternative answer for those hosting PHP on a Windows environment using IIS, remember you'll need a web.config file with the rewrites configured to serve Laravel:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>

    <rewrite>
      <rules>
        <rule name="Imported Rule 1" stopProcessing="true">
          <match url="^(.*)/$" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Redirect" redirectType="Permanent" url="/{R:1}" />
        </rule>
        <rule name="Imported Rule 2" stopProcessing="true">
          <match url="^" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php" />
        </rule>
      </rules>
    </rewrite>

    </system.webServer>
</configuration>
Anthony
  • 1,776
  • 17
  • 29