简体   繁体   中英

.htaccess: remove public from URL

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

/app/ .htaccess

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.

/app/public/ .htaccess

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM