如何使用.htaccess从URL隐藏文件夹名称

时间:2022-09-26 10:43:07

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.

我有一个根文件夹'www'的网站,但我把所有的php文件包括index.php放在root的子文件夹中。我给自己写了一个.htaccess文件来重定向,所以如果我输入www.test.com,它会跳转到www.test.com/folder并显示index.php。

Below it's my .htaccess which I put in the root.

下面是我放入根目录的.htaccess。

 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

Right now, I want to change the .htaccess file to reach these goals:

现在,我想更改.htaccess文件以达到以下目标:

  1. When I input www.test.com, jump to www.test.com/folder as usual, but display the url without folder.
  2. 当我输入www.test.com时,像往常一样跳转到www.test.com/folder,但显示没有文件夹的URL。

  3. All the pages in folder will display the url without folder name. Such as

    文件夹中的所有页面都将显示没有文件夹名称的URL。如

    www.test.com/shop/page1 -> www.test.com/page1

    www.test.com/shop/page1 - > www.test.com/page1

I searched some of the scripts, but none of them works.

我搜索了一些脚本,但没有一个可以工作。

3 个解决方案

#1


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]

Reference: https://*.com/a/18361995/3673491

#2


Based on your current rules. You can just make a slight adjustment.

根据您当前的规则。你可以稍微调整一下。

 RewriteEngine On
 RewriteBase /
 #ignore folder2 in which I put important files, but no works for the website
 RewriteRule ^folder2/? - [L] 

 RewriteRule ^/?$ /folder/ [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ /folder/$1 [L]

#3


You can use below code.

您可以使用以下代码。

RewriteEngine On

RewriteBase /

RewriteRule ^/?$ /folder/ [L]

RewriteRule ^ /?$ / folder / [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond%{REQUEST_FILENAME}! - f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond%{REQUEST_FILENAME}!-d

RewriteRule ^(.*)$ /folder/$1 [L]

RewriteRule ^(。*)$ / folder / $ 1 [L]

#1


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]

Reference: https://*.com/a/18361995/3673491

#2


Based on your current rules. You can just make a slight adjustment.

根据您当前的规则。你可以稍微调整一下。

 RewriteEngine On
 RewriteBase /
 #ignore folder2 in which I put important files, but no works for the website
 RewriteRule ^folder2/? - [L] 

 RewriteRule ^/?$ /folder/ [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ /folder/$1 [L]

#3


You can use below code.

您可以使用以下代码。

RewriteEngine On

RewriteBase /

RewriteRule ^/?$ /folder/ [L]

RewriteRule ^ /?$ / folder / [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond%{REQUEST_FILENAME}! - f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond%{REQUEST_FILENAME}!-d

RewriteRule ^(.*)$ /folder/$1 [L]

RewriteRule ^(。*)$ / folder / $ 1 [L]