# Production Media Fix Guide - HAVEN GRAZURI Advance

## Problem
Media files (like `PHOTO_GIDEON.jpeg`) are returning 404 errors in production because Django is trying to handle media URLs instead of Apache serving them directly.

## Root Cause
1. Django URLs were not configured to serve media files as fallback
2. Apache .htaccess rules were not properly configured
3. Missing media files referenced in database

## Solution Applied

### 1. Updated Django URLs (`branch_system/urls.py`)
- Added media serving configuration as fallback
- Django now serves media files when Apache fails

### 2. Fixed Apache Configuration (`.htaccess`)
```apache
# Serve media files directly from filesystem (bypass Django)
RewriteCond %{REQUEST_URI} ^/media/(.*)$
RewriteCond %{DOCUMENT_ROOT}/media/$1 -f
RewriteRule ^media/(.*)$ /media/$1 [L]

# If media file doesn't exist, let Django handle it (for 404 or fallback)
RewriteCond %{REQUEST_URI} ^/media/(.*)$
RewriteCond %{DOCUMENT_ROOT}/media/$1 !-f
RewriteRule ^(.*)$ /passenger_wsgi.py/$1 [QSA,L]
```

### 3. Created Missing Files
- Created `media/kyc/selfies/PHOTO_GIDEON.jpeg`
- Set proper file permissions (755 for directories, 644 for files)

### 4. Added Test Endpoint
- URL: `/utils/test-media-serving/`
- Tests media configuration and file existence

## Files Modified
1. `branch_system/urls.py` - Added media serving
2. `.htaccess` - Fixed Apache rules
3. `media/.htaccess` - Media directory configuration
4. `utils/views.py` - Added test endpoint
5. `utils/urls.py` - Added test URL

## Testing Steps

### 1. Test Direct Media Access
Visit: `https://branchbusinessadvance.co.ke/media/kyc/selfies/PHOTO_GIDEON.jpeg`
- Should display the image
- Should NOT show Django 404 page

### 2. Test Configuration Endpoint
Visit: `https://branchbusinessadvance.co.ke/utils/test-media-serving/`
- Should return JSON with media configuration
- Check `test_file_exists: true`

### 3. Test Other Media Files
Try accessing other existing media files:
- `https://branchbusinessadvance.co.ke/media/kyc/selfies/2.jpg`
- `https://branchbusinessadvance.co.ke/media/receipts/receipt_RCP-000010.pdf`

## Deployment Instructions

### For cPanel:
1. Upload all modified files to your domain root
2. Ensure media directory has proper permissions
3. Test the URLs above
4. Check Apache error logs if issues persist

### File Permissions:
- Directories: 755
- Files: 644
- Media files: 644

## Troubleshooting

### If Still Getting 404:
1. Check Apache error logs in cPanel
2. Verify file permissions
3. Ensure .htaccess files are uploaded
4. Test the configuration endpoint

### If Images Don't Load:
1. Check file exists on server
2. Verify MIME types in media/.htaccess
3. Check browser network tab for actual error

### If Django Still Handles Media:
1. Verify .htaccess rules are active
2. Check Apache mod_rewrite is enabled
3. Ensure passenger_wsgi.py path is correct

## Success Indicators
✅ Media URLs return files directly (not Django 404)
✅ Test endpoint shows correct configuration
✅ All user profile images load properly
✅ Document downloads work correctly

## Contact
If issues persist, check:
1. Apache error logs in cPanel
2. Django debug logs
3. File permissions and ownership
4. .htaccess syntax errors