以下是我的网站目录结构:
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在本地主机和友好url上工作的响应进行了一些更改。
目录结构:
- /(本地主机根目录)
- myapp/
- 公共/
- index . php
核心/
- myapp/
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/公共/. htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
myapp/公共/index . php
<?php
echo 'Hello<br>';
echo $_SERVER['QUERY_STRING'];
一些请求:
http://localhost/myapp/你好
http://localhost/myapp/post/new
你好post/新
这对我来说一直都很有效。在你的根目录中创建一个。htaccess,然后添加这3行代码,问题就解决了。
RewriteEngine On
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
我正在为我的"宠物项目"寻找相同的解决方案。Infinityfree.
目录结构(前):
htdocs/
├─ public/
└─ src/
以上答案都没有单独帮助我,导致404或503 http错误或只是显示我的项目文件夹。
SandroMarques的回答鼓励我尝试添加两个。htaccess 文件。经过多次尝试,我找到了一个适合我的解决方案。我使用了anubhava的和SandroMarques的答案。
目录结构(后):
htdocs/
├─ public/
│ └─.htaccess //second file
│
├─ src/
└─.htaccess //first file
第一个。htaccess文件:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /public/([^'s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
第二个htaccess文件:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
在symfony 5中,如果你想用。htaccess重定向到public/index。php文件你可以这样做:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ public/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php [QSA,L]
如果你有加密证书,你可以添加这个重定向到HTTPS,例如:RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]