Forum Moderators: phranque

Message Too Old, No Replies

RegEx help

         

keyplyr

3:18 am on Jun 6, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



They both result in the desired 410 Gone server response. Any advantage to the longer version?

RewriteRule ^example.html$ - [G]
RewriteRule ^example-widgets.html$ - [G]

RewriteRule ^example(-widgets)?.html$ - [G]

lucy24

4:35 am on Jun 6, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Config or htaccess? If it's in htaccess, the RegEx in the single-line version has to be recompiled on every request. Now, whether this creates a heavier load on the server than the alternative task of processing two separate RewriteRules... There may exist benchmark studies of the time and CPU load involved, but to some extent it's going to be specific to the current server. And I strongly suspect (as I'll bet you do too) that the difference in normal circumstances will be insignificant. So you're better off using whichever form you, personally, will feel most comfortable working with.

You will, of course, say \. for . because this is the rare case where it actually makes a difference. With a pattern expressed as "example.html", using dot (= any character) rather than \. (= literal period), a request for "example-widgets.html" will continue to match the pattern for one character longer than it ought to--and then just watch those picoseconds adding up.

If you go with the two-rule version, list the two pages in order of likelihood-to-be-requested.

Now, depending on what, exactly, "example" stands for, you may have a third and definitely more efficient option:

RewriteRule ^example - [G]
without closing anchor.

keyplyr

4:55 am on Jun 6, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This is htaccess. The servers I use don't face the public (just your servers... he he)
RewriteRule ^example - [G]

without closing anchor.
Yup, that's the one. Short & covers several file types that are also gone. I was unsure of how liberal I could be with the regex for a 410. So there's no more negotiation restraints than on any other rule huh? It's not too often I use a 410.

Much grass

lucy24

5:04 pm on Jun 6, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



the regex for a 410

A pattern is a pattern and a flag is a flag and never the twain shall meet.
Oh. Wait. Wasn't he actually saying that the twain meet all the time? Never mind then.

<digression>
The question made me think of my real-life version, which involves a directory along the lines of
/artists/john.html
/artists/john_files/{buncha-jpgs-here}
/artists/mary.html
/artists/mary_files/{buncha-jpgs-here}
et cetera. By and by when it came time to remove one person from the site (we didn't kick him out, he died) the rule was thriftily expressed as
RewriteRule ^artists/richard - [G]
and that's all the visitor needs. So I know from experience that it works.
</digression>

jmccormac

7:48 pm on Jun 6, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The conditional version may require more processing. Simple RegExps are generally processed quicker. It might not have any performance impact on a low traffic server though.

Regards...jmcc

keyplyr

9:12 pm on Jun 6, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks jmccormac