简体   繁体   中英

htaccess 301 redirect regex

I am trying to 301 redirect this format or url

mydomain.com/texas/map-of-texas/

to

mydomain.com/texas/

I have a lot of urls of states and cities with this pattern so I need a regex rule but I can't seem to get the regex syntax right.

I have used this and similar. What am I doing wrong?

RewriteRule ^/map-of(.+)  mydomain.com /$1 [R=301,L]

The ^ matches the start of the path portion of the url, but in your example "map-of" is not at the beginning of the url. Furthermore, mod_rewrite paths do not start with a / , but directly with the url path.

A mod_rewrite rule that rewrites all urls containing "map-of-" looks like this:

RewriteRule (^|/)map-of-(.+?)(/|$)  /$2 [R=301,L]

The ? means that the match is not "greedy".

The rewrite rule redirects the following type of urls

mydomain.com/texas/map-of-texas/
mydomain.com/texas/map-of-texas/123
mydomain.com/map-of-texas

to

mydomain.com/texas

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