简体   繁体   English

htaccess-获取网址的最后一句话

[英]Htaccess - get last word of url

I want to implement a .htaccess redirect based on the following pattern: 我想基于以下模式实现.htaccess重定向:

www.site.com/ugly-depreciated-values-name

to

www.site.com/location/name
  • The value after the final hyphen will always be used as the new url. 最终连字符后的值将始终用作新网址。
  • There is not a constant number of hyphens in the old urls. 旧网址中的连字符数量不固定。
     import re
     x="www.site.com/ugly-depreciated-values-name"
     print re.sub(r"(.*?/).*?-(\w+)$",r"\1location/\2",x)

Output:www.site.com/location/name 输出:www.site.com/location/name

This is in Python. 这是在Python中。

You can use this rule in your root .htaccess: 您可以在根.htaccess中使用此规则:

RewriteEngine On

RewriteRule ^.+?-([^/-]+)/?$ /$1 [L,R=302]
RewriteEngine On
RewriteRule ^[A-Z0-9a-z\-]+-(.*)$ http://www.example.com/location/$1 [R=301,L]

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

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