如何使用 .htaccess 从 URL 中隐藏文件夹名称 [英] How can I hide folder name from URL using .htaccess

查看:79
本文介绍了如何使用 .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:

  1. 当我输入www.test.com时,像往常一样跳转到www.test.com/folder,但显示的url没有文件夹.
  2. 文件夹中的所有页面将显示没有文件夹名称的网址.比如

  1. When I input www.test.com, jump to www.test.com/folder as usual, but display the url without folder.
  2. 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]

参考:https://stackoverflow.com/a/18361995/3673491

这篇关于如何使用 .htaccess 从 URL 中隐藏文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆