簡體   English   中英

.htaccess 從站點根目錄重定向到公共文件夾,在 URL 中隱藏“公共”?

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

波紋管是我的網站目錄結構:

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

我不希望任何用戶直接訪問src目錄中的任何文件。 src包含我的模板和后端邏輯腳本。

我想設置我的.htaccess以便當用戶訪問我的網站(根文件夾My-Project )時,他們被重定向到My-Project/public/index.php 我希望 URL 在索引頁面上看起來像My-Project.com

有什么幫助嗎? 我很猶豫要不要修改我的 .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

我對@anubhava 對在 localhost 和友好 URL 上工作的響應進行了一些更改。

目錄結構:

  • /(本地主機根文件夾)
    • 我的應用程序/
      • 民眾/
      • 索引.php
    • 核/
      • 一些核心文件...

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]

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

RewriteEngine On
RewriteBase /

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

myapp/public/index.php

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

一些要求:

http://localhost/myapp/

你好


http://localhost/myapp/post/new

你好

發布/新

這一直對我有用。 在公共文件夾外的根目錄中創建一個 .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