如何使用.htaccess删除部分URl?
目前,我的网站上有 20 多个采用这种格式的 URL
http://www.example.net /content/index/mission
我想从所有 URL 中删除 /content/index,因此它们应该看起来像这样
http://www.example.net/mission
换句话说,我总是会从 URL 中删除 /content/index 。 我确信这确实很简单,但我对 Apache 不是很有经验。
Currently, I have 20+ URLs on my site in this format
http://www.example.net/content/index/mission
I want to remove /content/index from all URLs, so they should look something like this
http://www.example.net/mission
In other words, I would always remove /content/index from the URL.
I'm sure it's really straightforward, but I'm not very experienced with Apache.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要 Apache 的重写模块:
mod_rewrite
。然后做这样的事情:
这是mod_rewrite的官方文档:点击
You would need the rewrite module of Apache:
mod_rewrite
.Then do something like this:
Here is the official documentation of mod_rewrite: click
我猜您已经为 http://www. example.net/content/index/mission
您需要找到这些规则并添加一个新规则,该规则使用类似的结构,但对内容/索引部分进行“硬编码”,例如,假设现有规则是
“您” d 想制定一个新规则来选择 /mission 并将其重写为类似的方式,但是在现有规则生效之前,例如,
这些只是示例 - 这实际上取决于您现有的规则是什么。
I'm going to guess you already have some rewrite rules in place for URLs like http://www.example.net/content/index/mission
You need to find these rules and add a new one which uses a similar structure but 'hard codes' the content/index parts, for example, suppose the existing one was
You'd want to make a new rule to pick up /mission and rewrite it in a similar way, but before the existing rule kicks in, e.g.
These are just examples - it will really depend on what your existing rules are.
或者只需添加index.html,具体取决于您要删除的扩展名,并且Apache 上需要MOD_REWRITE 才能实现此操作。谢谢。
Or just add index.html depending on whatever extention you want to remove and MOD_REWRITE is required on Apache for this to work. Thank you.
我还有一个我编写的脚本,您可以将其放在服务器上,然后通过互联网浏览器浏览到它,它可以确认您的服务器上是否有 mod_rewrite 。只要启用了 mod_rewrite,我向您展示的方法就可以 100% 工作
I also have a script I wrote that you put on your server then browse to it via internet browser and it can confirm if you have mod_rewrite on your server. The way I showed you works 100% as long as mod_rewrite is enabled