0

I have created Laravel CRUD API and it works fine in localhost(without .htaccess file), but it fails on the live server. When I send a request it returns 404 not found error, or downloads a file depending on .htaccess content. How should I change .htaccsess file for makeing code to work. this is my .htaccess file which returns not found error.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
1
  • Very dangerous: if your virtual host is pointed to root of project everyone from the internet will be able to read your .env file with credentials. RewriteRule ^(.*)$ public/$1 [L] says that your virtual host setting is that way. Commented Mar 30, 2020 at 14:17

1 Answer 1

1

There is an 'official' htaccess that should provide you with the basic code to get this working.

See https://github.com/laravel/laravel/blob/master/public/.htaccess

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

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Then, simply make sure that the root points to yourproject/public so it can boot only from yourproject/public/index.php.

Sign up to request clarification or add additional context in comments.

4 Comments

yes now it works, but with my .htaccess code it works too. is there any way to send requests without /public/ in it?
You nee to define your root directory as public. This is either some setting in your webhost panel like plesk/cpanel, or is defined in apache
I have deleted the project and clone it again from Github repo. Now it downloads public/index.php file with the same .htaccess file. Have I done any mistake in process
public should never be part of the URL. If it is then youre doing something wrong. This is server config and not code config.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.