简体   繁体   中英

Htaccess - get last word of url

I want to implement a .htaccess redirect based on the following pattern:

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

This is in Python.

You can use this rule in your root .htaccess:

RewriteEngine On

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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