I am setting up my Laravel API on Heroku. Everything seems to be working fine, I am able to go to the site and see the Laravel public page. But when I try to access my API routes (signup, login, etc.), it returns a 404 error.
The routes work fine on my own virtual hosts that I created but once I uploaded it to Heroku, all the routes except for "/" return 404. I've checked to see if "index.php/" would work but still returns a 404.
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]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_URI} 1^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Procfile
web: vendor/bin/heroku-php-apache2 public/
routes/api.php
$api = app('Dingo\Api\Routing\Router');
$api->version("v1", function ($api) {
$api->get("/", function () {
return response()->json([
'status' => 'success'
], 200);
});
$api->post("/signup", "MusicShare\Http\Controllers\AuthController#signup");
})
Heroku logs
2019-06-16T23:04:37.778061+00:00 app[web.1]: 10.35.223.16 - - [16/Jun/2019:23:04:37 +0000] "GET /index.php/routes HTTP/1.1" 200 47843 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36
My Heroku logs do not return an error. Everything shows the status code as 200 but I still get a 404 in Chrome if I try to access "/api/signup" or "/signup" or "/index.php/signup"
if you are using apache LAMP server try this code
go to the conf file and edit this
i am using // for comminting for you
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
<Directory "/var/www/html"> // here you project path
Options Indexes FollowSymLinks
AllowOverride All // alow all permissions
Require all granted
</Directory>
and server restart
for this command sudo service apache2 restart
then try this command for rewrite
sudo a2enmod rewrite
and your sub routes working perfictlly
dont't forgot this command sudo a2enmod rewrite
You have to edit the config vars --- added API_PREFIX and API_DOMAIN into the Heroku settings for my site.
Related
currently, deploy a Laravel 8 project on an azure server via apache2, inside /var/www/html/Project. I have placed all the files inside the public on the root of the project and edited the index.php file's autoload.php & app.php according to the project path, which works, and the welcome page on URL / display the page correctly.
when I click on the button to navigate to another page, which displays 404 but routes are exist and working on localhost.
when submitting a post via Axios that also doesn't works which shows 404, that works on localhost.
following is the .htaccess 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]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
why my laravel url not working when method is post
example the url api must be : http://localhost/PROJECTNAME/api/loginWeb
but when i click post button it always direct to http://localhost/api/loginWeb
but when method is get this okay. i use xampp
here is my 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]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
When we're using session based laravel setup, all the route generator functions(url(), route()) use hostname http://localhost as root url, not http://localhost/PROJECTNAME. To resolve this issue please do following changes in laravel app.
Define APP_URL in .env file as APP_URL="http://localhost/PROJECTNAME"
Go to RouteServiceProvider which is located at app/Providers/RouteServiceProvider and force laravel to use APP_URL as root url for your app.
public function boot()
{
parent::boot();
// Add following lines to force laravel to use APP_URL as root url for the app.
$strBaseURL = $this->app['url'];
$strBaseURL->forceRootUrl(config('app.url'));
}
Same issue occurred to me when I used same domain and a suburl to identify different projects.
Linux: Nginx serves multiple apps with two different locations
Windows: Multiple Laravel Applications Using Nginx - Windows
Im running a Lumen app on Elastic beanstalk
The index (home) page works but non of the others
Base url returns the index page. "Base url + /info" gives me 404
Iv checked the logs and can see nothing mentioned. Iv given the storage folder 777 permissions, also no joy. which is strange since the index page has been cached and is working.
The app works perfectly locally
Am I missing something obvious?
Thanks!
*****EDIT*****
More info:
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]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
routes
$router->get('/', 'HomeController#index');
$router->get('info', 'HomeController#info');
HomeController
public function index() {
return view('index');
}
public function info() {
return view('info');
}
Both the index and Info pages extend the app layout page which is located in resources/views/layouts
On an Ngingx server, such as Elastic beanstalk is, the server config has to be edited as described here:
https://lumen.laravel.com/docs/5.1
In the server array add:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
My code works perfectly fine while I am using it on my local and I can get the user info with postman and I have no issues getting the access token from server or localhost but using the access token for retrieving data from my real server always returns { message: unauthenticated } with status 401
The reason I found out is that Apache Drops the Authorize Header ! I have tried to fix it with adding these to my .htaccess but they don't do any help
my Apache vr is 2.4.29
this is my .htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Someone please help me with this... :(
It seems the important thing I didn't know was that in first: some shared hosting websites don't allow you to access or modify the apache config file, and second: in the config file its needed to change the AllowOverride from None to All
AllowOverride All
I switched from a WH4S shared host to a Dreamhosting VPS and everything is working just as it should have been.
I'm using a shared hosting server, I installed my first Laravel 5.4 application and everything is still working fine. A few days ago I installed another Laravel 5.4 application on a different domain, but same server, but when I try to access the URL, I get 500 INTERNAL SERVER ERROR.
Below is my .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
I also tried the following, but same error:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
### Add two lines for fix error 500 ###
Options -Indexes
php_flag xcache.cacher 0
#######################################
# 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>
You moved everything inside your public_html folder which you should not do. Only files inside /public needs to be in public_html, hence the name public. The Laravel files should be placed higher directory where it is not accessible from the public.
Install Laravel with Softaculous, or with composer via SSH in /home/username/laravel
Move everything in /home/username/public_html folder to
/home/username/laravel
Move everything in /home/username/laravel/public to /home/username/public_html and delete the public folder after move.
Edit this file: /home/username/public_html/index.php
Change to require '/home/username/laravel/vendor/autoload.php';
Change to $app = require_once '/home/username/laravel/bootstrap/app.php';
Save this file and close window.
Edit this file: /home/username/laravel/server.php
Change to:
if ($uri !== '/' && file_exists('/home/username/public_html'.$uri)) {
return false;
}
require_once '/home/username/public_html/index.php';
Replace username with your hosting username. :)