Home > database >  How to configure .htaccess for a Laravel API app which is hosted on a subdomain?
How to configure .htaccess for a Laravel API app which is hosted on a subdomain?

Time:10-24

I have a domain mydomain.com and a nginx space to host my website. The URL address mydomain.com is already in use and I don't want to buy another domain, so I created a subdomain myapp.mydomain.com. Then I created a laravel API app myapp for testing purposes and uploaded the code into /mydomain_com/myapp folder via a FTP client.

The problem now is I have to configure my .htaccess accordingly so the the app can run correctly.

At the current state, the homepage route / is working fine, but everything else is not working.

\mydomain_com\myapp.env:

...
APP_NAME=myapp
APP_ENV=local
APP_KEY=base64:U4v....
APP_DEBUG=false
APP_URL=https://myapp.mydomain.com
...

My routes are: Laravel API app routes

\mydomain_com\myapp\routes\web.php

Route::get('/', function () {
    return view('welcome');
});
Route::get('/aaa', function () {
    return view('welcome');
});

\mydomain_com\myapp\routes\api.php

Route::apiResource('os', OsController::class);

Now, when I go to https://myapp.mydomain.com I get this:

homepage route

However when I try to visit https://myapp.mydomain.com/aaa and https://myapp.mydomain.com/api/os, I get this error ↓

/aaa route

Can You pls tell me what's the problem with my \mydomain_com\myapp\.htaccess ↓ ? Why is the homepage only working?

RewriteEngine On

RewriteBase /

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

RewriteCond %{HTTP_HOST} !^subdomain
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

RewriteCond %{HTTP_HOST} ^subdomain
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/ [L]

CodePudding user response:

Im using the htaccess which is already with the laravel and Im posting it below, it works fine with me

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

Also check if the mod_rewrite is enabled in your server, if you are using ubuntu and apache2 server enable the mod_rewrite using the command

sudo a2enmod rewrite

Kindly refer : Enable mod_rewrite

CodePudding user response:

Update the content of .htaccess file in the root folder with this. It should work.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Removes index.php from ExpressionEngine URLs
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

    # Directs all EE web requests through the site index file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
  • Related