简体   繁体   中英

htaccess redirectmatch regular expression to avoid infinite loop

I have a malformed link somewhere which sends people to my sitemap but to a URL like

www.example.com/sitemap.php'"abc def 

etc. This then presents them with broken links.

While I hunt down the bad link, I wrote a 301 like this to make all traffic to the page be 301ed and cleaned up:

RedirectMatch 301 ^sitemap.php(.*) http://www.example.com/sitemap.php

But this gives me a "too many 301s" errors because site.com/sitemap.php matches the expression and it gets looped. Is there a regular expression that I can use instead of (.*) that will not match if there is nothing there at all (ie if the URL just ends with ".php"?

Just use this rule:

RedirectMatch 301 ^/(sitemap\.php).+ /$1

And clear your browser cache. Note use of .+ (1 or more of any character) instead of .* (0 or more chars)

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