如何使用.htaccess删除部分URl?

发布于 2024-12-09 09:42:24 字数 402 浏览 1 评论 0原文

目前,我的网站上有 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 技术交流群。

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

发布评论

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

评论(5

江城子 2024-12-16 09:42:24

您需要 Apache 的重写模块:mod_rewrite

然后做这样的事情:

RewriteEngine on 
RewriteRule ^content/index/(.*)$ $1

这是mod_rewrite的官方文档:点击

You would need the rewrite module of Apache: mod_rewrite.

Then do something like this:

RewriteEngine on 
RewriteRule ^content/index/(.*)$ $1

Here is the official documentation of mod_rewrite: click

唔猫 2024-12-16 09:42:24
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/removedstring/
RewriteRule ^removedstring/(.*)$ https://www.domain.eu/$1 [L,NC,R=301]
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/removedstring/
RewriteRule ^removedstring/(.*)$ https://www.domain.eu/$1 [L,NC,R=301]
心房敞 2024-12-16 09:42:24

我猜您已经为 http://www. example.net/content/index/mission

您需要找到这些规则并添加一个新规则,该规则使用类似的结构,但对内容/索引部分进行“硬编码”,例如,假设现有规则是

RewriteRule ^content/(.*)/(.*)$ /content.php?param1=$1¶m2=$2 [L,qsa]

“您” d 想制定一个新规则来选择 /mission 并将其重写为类似的方式,但是在现有规则生效之前,例如,

RewriteRule ^mission$ /content.php?param1=index¶m2=mission [L,qsa]
RewriteRule ^content/(.*)/(.*)$ /content.php?param1=$1¶m2=$2 [L,qsa]

这些只是示例 - 这实际上取决于您现有的规则是什么。

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

RewriteRule ^content/(.*)/(.*)$ /content.php?param1=$1¶m2=$2 [L,qsa]

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.

RewriteRule ^mission$ /content.php?param1=index¶m2=mission [L,qsa]
RewriteRule ^content/(.*)/(.*)$ /content.php?param1=$1¶m2=$2 [L,qsa]

These are just examples - it will really depend on what your existing rules are.

_蜘蛛 2024-12-16 09:42:24
  # Get rid of index.php
  RewriteCond %{REQUEST_URI} /index\.php
  RewriteRule (.*) index.php?rewrite=2 [L,QSA]

  # Rewrite all directory-looking urls
  RewriteCond %{REQUEST_URI} /$
  RewriteRule (.*) index.php?rewrite=1 [L,QSA]

或者只需添加index.html,具体取决于您要删除的扩展名,并且Apache 上需要MOD_REWRITE 才能实现此操作。谢谢。

  # Get rid of index.php
  RewriteCond %{REQUEST_URI} /index\.php
  RewriteRule (.*) index.php?rewrite=2 [L,QSA]

  # Rewrite all directory-looking urls
  RewriteCond %{REQUEST_URI} /$
  RewriteRule (.*) index.php?rewrite=1 [L,QSA]

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.

林空鹿饮溪 2024-12-16 09:42:24

我还有一个我编写的脚本,您可以将其放在服务器上,然后通过互联网浏览器浏览到它,它可以确认您的服务器上是否有 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文