繁体   English   中英

如何使用.htaccess从URL末尾删除数字?

[英]How to remove a number from the end of an URL using .htaccess?

我有很多以-xxx结尾的URL,其中xxx是介于000和999之间的数字。例如:

http://a.com/imagea-000
http://a.com/category1/imageb-002
http://a.com/category1/category2/imagec-999

如何在.httaccess中指定RewriteCond / RewriteRule以删除数字,包括url末尾的“-”? 因此,最终的URL如下所示:

http://a.com/imagea
http://a.com/category1/imageb
http://a.com/category1/category2/imagec

您可以在DOCUMENT_ROOT/.htaccess文件中使用以下代码:

RewriteEngine On

RewriteRule ^(.+?)-\d+/?$ /$1 [L,R=302]
  • -\\d+/?$将匹配连字符,后跟1或数字和URI末尾的可选斜杠。
  • (.+?)将匹配上述模式之前的任何内容并将其捕获为组#1
  • 确认其工作正常后,将R=302替换为R=301 在测试mod_rewrite规则时,避免使用R=301 (永久重定向)。

暂无
暂无

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

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