# Laravel API 404 Error Fix - cPanel Deployment

## Problem

The API endpoint `https://amostore.in/api/register` returns 404 Not Found, even though the route exists in `routes/api.php`.

## Root Cause

**cPanel document root is set to `public_html`**, but Laravel's entry point (`index.php`) is in the `public` folder. This mismatch causes all Laravel routes to return 404 because Apache/Nginx cannot find the files.

---

## Solution A: Change Document Root (RECOMMENDED)

This is the cleanest and most secure approach.

### Steps:

1. Log into cPanel
2. Navigate to **Domains** → **Addon Domains** (or **Subdomains** if using subdomain)
3. Find `amostore.in` in the list
4. Click **Edit**
5. Change **Document Root** from `public_html` to:
    ```
    /home/your_cpanel_username/amos/public
    ```
    (Replace `your_cpanel_username` with your actual cPanel username)
6. Click **Save**

### After Changing Document Root:

-   Ensure the `.htaccess` file from `amos/public/.htaccess` is present at the new document root
-   Test: `https://amostore.in/api/register` should now work

---

## Solution B: Move Files to public_html

If you cannot change the document root, move Laravel's public files to `public_html`.

### Step 1: Upload Files to Live Server

Using FTP, File Manager, or SSH, copy these files from your local machine to the server:

```
From local: amos/public/*
To server:  public_html/
```

Specifically copy:

-   `index.php`
-   `.htaccess`
-   `assets/` folder (and all its contents)
-   `build/` folder (if exists)
-   `vendor/` folder (if exists in public)
-   `favicon.ico`, `robots.txt`, etc.

### Step 2: Adjust index.php Paths

Edit `public_html/index.php` on the server and change:

```php
// Change FROM:
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';

// Change TO (if Laravel root is one level up from public_html):
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';

// OR if Laravel root is in a subdirectory like 'amos':
require __DIR__.'/../amos/vendor/autoload.php';
$app = require_once __DIR__.'/../amos/bootstrap/app.php';
```

### Step 3: Folder Structure on Server

Your final structure should look like:

```
/home/username/
├── amos/                    (Laravel project root)
│   ├── app/
│   ├── bootstrap/
│   ├── config/
│   ├── database/
│   ├── resources/
│   ├── routes/
│   ├── storage/
│   ├── vendor/
│   ├── .env
│   └── composer.json
│
└── public_html/             (Document Root - symlink or copy of amos/public)
    ├── index.php            (adjusted paths)
    ├── .htaccess
    ├── assets/
    ├── build/
    └── uploads/
```

### Step 4: Set Permissions

```bash
chmod -R 755 /home/username/amos/storage
chmod -R 755 /home/username/amos/bootstrap/cache
```

### Step 5: Clear Laravel Caches

SSH into server or use cPanel Terminal:

```bash
cd /home/username/amos
php artisan route:clear
php artisan config:clear
php artisan cache:clear
php artisan optimize:clear
```

---

## Solution C: Create Symbolic Link (Advanced)

If you have SSH access and want to keep `public_html` as document root but use Laravel's `public` folder:

```bash
cd /home/username/public_html
rm -rf *  # WARNING: Backup first!
ln -s /home/username/amos/public/* .
ln -s /home/username/amos/public/.htaccess .
```

---

## Verification

After applying any solution, test:

1. **Test API route:**

    ```
    https://amostore.in/api/register
    ```

    Should return JSON (405 for GET, but not 404)

2. **Test a simple GET API:**
   Add temporarily to `routes/api.php`:

    ```php
    Route::get('/test', function() {
        return response()->json(['status' => 'ok']);
    });
    ```

    Then visit: `https://amostore.in/api/test`

3. **Check web routes:**
    ```
    https://amostore.in/
    ```
    Should load your homepage, not 404.

---

## Common Issues After Fix

### Issue: 500 Internal Server Error

-   Check `storage/logs/laravel.log` for errors
-   Ensure `.env` file exists and has correct values
-   Run `composer install --optimize-autoloader --no-dev`
-   Run `php artisan key:generate` if APP_KEY missing

### Issue: Assets not loading

-   Update `APP_URL` in `.env` to `https://amostore.in`
-   Clear config cache: `php artisan config:cache`
-   Check file permissions on `storage/` and `public/assets/`

### Issue: Still 404 on API

-   Verify `.htaccess` is present in document root
-   Ensure Apache `mod_rewrite` is enabled
-   In cPanel → **Select PHP Version** → **Options**, ensure `public_html` is the correct path

---

## Quick Checklist

-   [ ] Document root points to `amos/public` OR files moved to `public_html`
-   [ ] `.htaccess` present in document root
-   [ ] `index.php` paths corrected (if using Solution B)
-   [ ] Laravel caches cleared on server
-   [ ] `APP_URL` set correctly in `.env`
-   [ ] File permissions set correctly
-   [ ] Apache `mod_rewrite` enabled (contact host if unsure)

---

## Need Help?

If still getting 404 after trying these solutions:

1. Share a screenshot of your cPanel **Domains** page showing document root
2. Check with your hosting provider: "Is Apache mod_rewrite enabled and AllowOverride All set for my domain?"
