如何使用 .htaccess 从 URL 中隐藏文件夹名称
我有一个根文件夹为www"的网站,但我将包括 index.php 在内的所有 php 文件放在根目录的子文件夹中.我自己写了一个 .htaccess 文件来重定向,所以如果我输入 www.test.com
,它会跳转到 www.test.com/folder
并显示 >index.php
.
I have a website with the root folder 'www', but I put all php files including index.php in a sub-folder of root.
I wrote myself a .htaccess file to redirect, so if I input www.test.com
, it will jump to www.test.com/folder
and display the index.php
.
下面是我放在根目录中的 .htaccess
.
Below it's my .htaccess
which I put in the root.
RewriteEngine On
RewriteBase /
RewriteRule ^folder2 - [L]
#ignore folder2 in which I put important files, but no works for the website
RewriteCond %{REQUEST_URI} !^/folder(.*)
RewriteRule (.*) /folder/$1
现在,我想更改 .htaccess 文件以实现这些目标:
Right now, I want to change the .htaccess file to reach these goals:
- 当我输入
www.test.com
时,像往常一样跳转到www.test.com/folder
,但显示的url没有文件夹. 文件夹中的所有页面将显示没有文件夹名称的网址.比如
- When I input
www.test.com
, jump towww.test.com/folder
as usual, but display the url without folder. All the pages in folder will display the url without folder name. Such as
www.test.com/shop/page1
-> www.test.com/page1
我搜索了一些脚本,但没有一个有效.
I searched some of the scripts, but none of them works.
我发现这个相关问题可以解决您的问题.答案的片段是(注意:它已根据您的需要进行调整):
I've found this related question which answers to your problem. The snippet from the answer is (note: it's adjusted to your needs):
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^folder/)^(.*)$ /folder/$1 [L,NC]