简体   繁体   English

.htaccess:从 URL 中删除 public

[英].htaccess: remove public from URL

Question问题

How can I remove /public/ from my URLs?如何从我的 URL 中删除/public/

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当我访问/app/about ,URL 更改为/app/public/about并且app/user/2更改为/app/user.php/?username=2

Setup设置

/app/ .htaccess /app/.htaccess

The .htaccess outside of the public folder contains:.htaccess公用文件夹之外包含:

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.当我访问localhost:8888/app/它会加载公共文件夹中的index.php文件。

/app/public/ .htaccess /app/public/.htaccess

The .htaccess inside of the public folder contains:.htaccess公共文件夹内包含:

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相关URL 重写与“公共”文件夹中的索引

Your # Add trailing slash rule appears to be problem.您的# Add trailing slash规则似乎有问题。 Can you try commenting it out for testing.您可以尝试将其注释掉以进行测试。

/app/.htaccess /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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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