使用的.htaccess从URL中删除字符串 [英] Removing string from URL using .htaccess

查看:134
本文介绍了使用的.htaccess从URL中删除字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 example.com/pages/page1 /example.com/pages/page2

有没有在的.htaccess 一个简单的方法来摆脱 /页/ 部分,所以我的网址主要有:

Is there an easy way in .htaccess to get rid of the /pages/ section, so my URLs are:

example.com/page1

感谢

推荐答案

如果您需要修改的能力的DocumentRoot ,这听起来像你只需要设置的DocumentRoot /路径/要/页。不过,如果你不能做到这一点,那么你可以在的.htaccess

If you have the ability to modify the DocumentRoot, it sounds like you would just need to set your DocumentRoot to /path/to/pages. However, if you can't do that then you can try this in .htaccess

RewriteEngine On
RewriteRule ^/(.*)$ /pages/$1 [L,QSA]

以上是通用的,重定向的一切 /页。如果您的网页确实被称为第一页第二页等等,那么这是更具体的:

The above is generic and redirects everything to /pages. If your pages really are called "page1 page2 etc", then this is more specific:

RewriteRule ^/page([0-9]+)$ /pages/page$1 [L,QSA]

删除 [L] 如果你有更多的重写规则来处理。该 [QSA] 转发以及可能已经present其他任何的查询字符串参数。

Remove the [L] if you have more rewrite rules to process. The [QSA] forwards along any other querystring parameters that may have been present.

修改:对于用户输入 example.com/pages/page1 被重定向到 example.com/第1页

EDIT: For users to enter example.com/pages/page1 to be redirected to example.com/page1:

RewriteEngine On
RewriteRule ^/pages/(.*)$ /$1 [L,QSA]

以上将在内部而不是重定向改变浏览器的地址栏。如果你想在地址栏更改,通知用户重定向发生,使用 [L,R = 301,QSA] 代替。

这篇关于使用的.htaccess从URL中删除字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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