Question
How can I remove /public/ from my URLs?
Problem
When I go to visit /app/about the URL changes to /app/public/about and app/user/2 changes to /app/user.php/?username=2
Setup
The .htaccess outside of the public folder contains:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /app/
# remove /public/ from URL
RewriteCond %{REQUEST_URI} !/public [NC]
RewriteRule ^(.*)/?$ public/$1
This works fine. When I visit localhost:8888/app/ it loads up the index.php file inside the public folder.
The .htaccess inside of the public folder contains:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# Add trailing slash
RewriteCond %{REQUEST_URI} ^(.+[^/])$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . %1/ [L,R=301]
# API Pages
# ------------------------------------------------------------
RewriteRule ^user/([a-z0-9]+)/?$ user.php?username=$1 [L,NC]
# Generic Pages
# ------------------------------------------------------------
RewriteRule ^about/?$ about.php [L,NC]
# Error Pages
# ------------------------------------------------------------
ErrorDocument 404 /404.php
Related: URL-rewriting with index in a "public" folder
Your # Add trailing slash rule appears to be problem. Can you try commenting it out for testing.
/app/.htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /app/
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+(.+?[^/])[?\s] [NC]
RewriteRule ^ /%1/ [L,R=301]
# remove /public/ from URL
RewriteCond %{REQUEST_URI} !/public/ [NC]
RewriteRule ^(.*?)/?$ public/$1 [L]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With