如何使用 .htaccess 更改网站的结构?
我正在对我的网络服务器进行一些重组,并且想改变我的结构的工作方式。目前,所有内容都存储在最低级别,因此当我访问我的网站时,网址如下所示:
www.example.com
www.example.com/page1.php
我的网站中有很多页面,我想将它们移出主文件夹。例如。
www.example.com/folder/
www.example.com/folder/page1.php
但是,我希望用户不要看到网址的 /folder/ 部分。换句话说,当用户访问我的网站时,我希望他导航到:
www.example.com/page1.php
www.example.com/myfolder1/page1.php
www.example.com/myfolder2/page2.php
www.example.com/anyfoldername/anypagename.php
但实际上
www.example.com/folder/page1.php
www.example.com/folder/myfolder1/page1.php
www.example.com/folder/myfolder2/page2.php
www.example.com/folder/anyfoldername/anypagename.php
我希望 url 始终显示而不显示 /folder。
有没有办法用 .htaccess 文件来做到这一点?
I'm doing some reorganizing on my web server and would like to change the way my structure works. Currently, everything is stored at the lowest level so that when I access my site the url looks like this:
www.example.com
www.example.com/page1.php
I have many pages in my site and i would like to move them out of the main folder. eg.
www.example.com/folder/
www.example.com/folder/page1.php
however, I would like for users not to see the /folder/ section of the url. In other words, when a user visits my site, I want him to navigate to:
www.example.com/page1.php
www.example.com/myfolder1/page1.php
www.example.com/myfolder2/page2.php
www.example.com/anyfoldername/anypagename.php
but actually be at
www.example.com/folder/page1.php
www.example.com/folder/myfolder1/page1.php
www.example.com/folder/myfolder2/page2.php
www.example.com/folder/anyfoldername/anypagename.php
I want the url to show without the /folder at all times.
Is there a way to do this with the .htaccess file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用mod_rewrite轻松完成此操作。例如:
注意:
该文件应放置在网站根文件夹中的 .htaccess 中。如果放置在其他地方,可能需要进行一些小的调整。
更新:
You can easily do this using mod_rewrite. For example:
NOTE:
This is to be placed in .htaccess in your website root folder. If placed elsewhere some small tweaking may be required.
UPDATE: