Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
999 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...