如何使用 .htaccess 更改网站的结构?

发布于 2024-12-02 12:17:21 字数 772 浏览 8 评论 0原文

我正在对我的网络服务器进行一些重组,并且想改变我的结构的工作方式。目前,所有内容都存储在最低级别,因此当我访问我的网站时,网址如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十级心震 2024-12-09 12:17:22
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]*)$ folder/$1 [L]
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]*)$ folder/$1 [L]
你是我的挚爱i 2024-12-09 12:17:21

您可以使用mod_rewrite轻松完成此操作。例如:

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteRule ^page1\.php$ /folder/page1.php [L]

注意:
该文件应放置在网站根文件夹中的 .htaccess 中。如果放置在其他地方,可能需要进行一些小的调整。


更新:

RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ /folder/$1 [L]

You can easily do this using mod_rewrite. For example:

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteRule ^page1\.php$ /folder/page1.php [L]

NOTE:
This is to be placed in .htaccess in your website root folder. If placed elsewhere some small tweaking may be required.


UPDATE:

RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ /folder/$1 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文