I have a wordpress blog where I have a series of posts with their own slugs. Recently I've noticed legitimate 404 errors where people go to mysite.com/my-slug/&somestuff. or mysite.come/my-slug/somestuff. I've no idea where they got 'somestuff' from but I want to be able to prevent that.
I wrote a regex to reroute from mysite.com/my-slug/anything to mysite.com/my-slug/
^/easy-fluffy-american-pancakes/.+
That works just fine. Well almost. As it turns out, my print-friendly view of any post is
^/easy-fluffy-american-pancakes/print/1234
1234 is the post ID. So now, that also gets redirected to the post. How can I exclude print from the redirect?
^/easy-fluffy-american-pancakes/(.+)|(?!print/[0-9]*/)
I can't quite get it to work. Either it doesn't match or I get too many redirects.
Close
^/easy-fluffy-american-pancakes/(?!print/[0-9]).+
Read (?....)
as "not followed by...". From the perspective of the outside of the (?....)
, (?....)
doesn't match anything, so the .+
starts matching immediately after the /
.
If you add an end of string check to the negative look ahead, it will also catch print
s with trailing garbage. If that's an issue.
print "foo/" =~ m{foo/(?!print/[0-9]+$).+} ? "redirect" : "pass";
pass
print "foo/9ujsdu" =~ m{foo/(?!print/[0-9]+$).+} ? "redirect" : "pass";
redirect
print "foo/print/1234" =~ m{foo/(?!print/[0-9]+$).+} ? "redirect" : "pass";
pass
print "foo/print/1234?o=1" =~ m{foo/(?!print/[0-9]+$).+} ? "redirect" : "pass";
redirect
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.