简体   繁体   English

如何使用 .htaccess 从最后一段中删除 URL 中的“索引”一词

[英]How to remove the word "index" in URL from the last segment using .htaccess

I want to remove word index from my URL as below我想从我的 URL 中删除单词索引,如下所示

from

https://domainname/what-we-do/index

to

 https://domainname/what-we-do/

I want to do this using .htaccess or anyhow.我想使用.htaccess或以任何方式执行此操作。

If it's literally just that one URL then you can do the following using mod_rewrite at the top of your .htaccess file:如果它实际上只是那个 URL 那么你可以使用.htaccess文件顶部的 mod_rewrite 执行以下操作:

RewriteEngine On

RewriteRule ^(what-we-do/)index$ /$1 [R=302,L]

The $1 backreference contains the captured subpattern (ie. what-we-do/ ) from the URL-path matched by the RewriteRule pattern . $1反向引用包含从与RewriteRule pattern匹配的 URL 路径中捕获的子模式(即what-we-do/ )。 This simply saves repetition in this instance.在这种情况下,这只是简单地节省了重复。

If you needed this to be more generic and redirect /<something>/index to /<something>/ then change the above RewriteRule directive to read:如果您需要它更通用并将/<something>/index重定向到/<something>/然后将上面的RewriteRule指令更改为:

RewriteRule ^([^/]+/)index$ /$1 [R=302,L]

This is a 302 (temporary) redirect.这是一个 302(临时)重定向。 If this is intended to be permanent then change this to a 301 - but only when you are sure it's working OK.如果这是永久性的,则将其更改为 301 - 但只有当您确定它工作正常时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM