php .htaccess 从站点根目录重定向到公共文件夹,在 URL 中隐藏“公共”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23635746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:51:05  来源:igfitidea点击:

.htaccess redirect from site root to public folder, hiding "public" in URL?

phpapache.htaccessmod-rewriteredirect

提问by Prefix

Bellow is my site directory structure:

波纹管是我的网站目录结构:

htdocs/
    My-Project/
        public/
            index.php
            css/
            img/
            js/
        src/
            config.php
            templates/
            library/

I do not want any direct user access to any files inside the srcdirectory. srccontains my templating and backend logic scripts.

我不希望任何用户直接访问src目录中的任何文件。src包含我的模板和后端逻辑脚本。

I want to set up my .htaccessso that when the user goes to my website (root folder My-Project), they are redirected to My-Project/public/index.php. I would like the URL to simply look like My-Project.comwhile on the index page.

我想设置我的,.htaccess以便当用户访问我的网站(根文件夹My-Project)时,他们被重定向到My-Project/public/index.php. 我希望 URLMy-Project.com在索引页面上看起来很简单。

Any help? I am hesitant to modify my .htaccess file as I see a lot of conflicting advice around here and on the net as to the best way to do this.

有什么帮助吗?我很犹豫要不要修改我的 .htaccess 文件,因为我在这里和网上看到了很多关于最佳方法的相互矛盾的建议。

回答by anubhava

Place this code in /My-Project/.htaccess:

将此代码放在/My-Project/.htaccess

RewriteEngine On
RewriteBase /My-Project/

RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]

RewriteRule ^((?!public/).*)$ public/ [L,NC]

回答by daxeh

Add the following to the first line of your .htaccess

将以下内容添加到 .htaccess 的第一行

DirectoryIndex public/index.php public/index.html

回答by SandroMarques

I made some changes to @anubhava's response to working on localhost and with friendly URLs.

我对@anubhava 对在 localhost 和友好 URL 上工作的响应进行了一些更改。

Directory structure:

目录结构:

  • / (localhost root folder)
    • myapp/
      • public/
      • index.php
    • core/
      • some core files ...
  • /(本地主机根文件夹)
    • 我的应用程序/
      • 民众/
      • 索引.php
    • 核/
      • 一些核心文件...

myapp/.htaccess(myapp root folder)

myapp/.htaccess(myapp 根文件夹)

RewriteEngine On
RewriteBase /myapp

RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^(.*)$ public/index.php? [L,QSA]

myapp/public/.htaccess

我的应用程序/公共/.htaccess

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php? [L,QSA]

myapp/public/index.php

myapp/public/index.php

<?php
echo 'Hello<br>';
echo $_SERVER['QUERY_STRING'];

Some requests:

一些要求:

http://localhost/myapp/

http://localhost/myapp/

Hello

你好



http://localhost/myapp/post/new

http://localhost/myapp/post/new

Hello

post/new

你好

发布/新