使用 .htaccess 从 URL 中删除字符串
我的网址格式为 example.com/pages/page1
和 /example.com/pages/page2
在 .htaccess
中是否有一种简单的方法code> 去掉 /pages/
部分,所以我的网址是:
example.com/page1
谢谢
I have URLs in the form of example.com/pages/page1
and /example.com/pages/page2
Is there an easy way in .htaccess
to get rid of the /pages/
section, so my URLs are:
example.com/page1
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您能够修改
DocumentRoot
,听起来您只需将DocumentRoot
设置为/path/to/pages
即可。但是,如果您不能这样做,那么您可以在.htaccess
中尝试。上面是通用的,并将所有内容重定向到
/pages
。如果您的页面确实被称为“page1 page2 等”,那么这更具体:如果您有更多重写规则要处理,请删除
[L]
。[QSA]
沿着可能存在的任何其他查询字符串参数进行转发。编辑:用户输入
example.com/pages/page1
将被重定向到example.com/page1
:以上将在内部重定向,但不更改浏览器的地址栏。如果您希望地址栏发生更改,通知用户重定向已发生,请改用
[L,R=301,QSA]
。If you have the ability to modify the
DocumentRoot
, it sounds like you would just need to set yourDocumentRoot
to/path/to/pages
. However, if you can't do that then you can try this in.htaccess
The above is generic and redirects everything to
/pages
. If your pages really are called "page1 page2 etc", then this is more specific:Remove the
[L]
if you have more rewrite rules to process. The[QSA]
forwards along any other querystring parameters that may have been present.EDIT: For users to enter
example.com/pages/page1
to be redirected toexample.com/page1
:The above will redirect internally but not change the browser's address bar. If you want the address bar to change, informing the user that the redirect has happened, use
[L,R=301,QSA]
instead.