简体   繁体   English

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

[英].htaccess redirect from site root to public folder, hiding “public” in URL?

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 src directory.我不希望任何用户直接访问src目录中的任何文件。 src contains my templating and backend logic scripts. src包含我的模板和后端逻辑脚本。

I want to set up my .htaccess so that when the user goes to my website (root folder My-Project ), they are redirected to My-Project/public/index.php .我想设置我的.htaccess以便当用户访问我的网站(根文件夹My-Project )时,他们被重定向到My-Project/public/index.php I would like the URL to simply look like My-Project.com while on the index page.我希望 URL 在索引页面上看起来像My-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 文件,因为我在这里和网上看到了很多关于最佳方法的相互矛盾的建议。

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/$1 [L,NC]

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

DirectoryIndex public/index.php public/index.html

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索引.php
    • core/核/
      • some core files ...一些核心文件...

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?$1 [L,QSA]

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

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [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发布/新

this works for me all the time.这一直对我有用。 Create a .htaccess in your root directory outside of the public folder then add these 3 lines and boom problem solve.在公共文件夹外的根目录中创建一个 .htaccess,然后添加这 3 行并解决问题。

RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

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

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