2

Currently when I remap my friendly to non-friendly URLs on my website, I normally use lines like these in my .htaccess:

RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteRule .* - [L]
RewriteRule ^afolder/subfolder/(.*)$ /internal.php?Q1=$1 [L]
RewriteRule ^afolder2/subfolder2/(.*)$ /internal.php?Q2=$1 [L]
....
RewriteRule ^something/else/(.*)$ /internal.php?Qn=$1 [L]

As you can see, I'm remapping http://example.com/whatever/whatever/value to http://example.com/internal.php?Q(something)=value.

The point I'm making is that with every RewriteRule statement, I'm used to starting the search with a caret and ending it with a dollar sign, even for the simplest rules. For example to map http://example.com/anything to http://example.com/something.php, I use:

RewriteRule ^anything$  /something.php [L]

My question is, when could I get away with NOT using carets or dollar signs to remap my URLs?

I'm asking because I want to boost the overall speed of my website, and every millisecond I shave off of processing time makes clients happier, (P.S. I need to shave off about 12ms loading time from the other end of North America then I'll meet the needs of google) and if I can remove those carets and/or dollar signs, then I feel I'll get a boost since the mod_rewrite engine will have a simpler string to process.

3
  • 1
    The carets and dollar signs are anchors. Without them, the match can happen anywhere along the value to be matched against. If you want a very specific match, one or more anchor should be used. If you want to match anywhere, then neither anchor should be used. There is little or no gain in speed in using anchors or not. It is better to use an anchor for safety. Anchors do not require recursion during matches so they are technically faster, though you are shaving extremely small amounts of time. Go for safety first, convenience second, speed last.
    – closetnoc
    Commented Jan 17, 2016 at 18:43
  • So technically removing an anchor is asking for trouble. Thanks. Commented Jan 17, 2016 at 18:47
  • Not always. It just means that a recursive match anywhere within the string can happen. If there is only one case, then it will match only once. But if you go temporarily senile or insane and forget a case, you can get more than one match. That is why I stress safety first. It can be more risky to not include at least one anchor (often the caret) unless you intend to match anywhere.
    – closetnoc
    Commented Jan 17, 2016 at 18:52

1 Answer 1

6

You are using the ^ and $ (anchors in regex speak) because you are matching the whole URL, which is what most people want to do, so this is the most common example you see.

If you omit the ^ and/or $ anchors then you are only going to be matching part of the URL. eg. anything$ is going to match "anything" at the end of the URL - this could match too many URLs (depending on your URL structure) and possibly allow invalid URLs to be matched. However, if you need to match "anything" at the end of the URL then this is the correct regex to use.

You basically need to use the appropriate regex for what you are trying to match. Speed/efficiency, for the most part, doesn't really come into it.

RewriteRule ^afolder/subfolder/(.*)$ /internal.php?Q1=$1 [L]

If you omitted the ^ anchor from this pattern then a user could potentially request /foo/afolder/subfolder/... and it will be caught by this rule.

Incidentally, the $ (end of string anchor) is superfluous here (because of the .* pattern) and can be omitted.

because I want to boost the overall speed of my website

Removing the anchors is not going to make any difference with speed. In fact, removing the ^ (start) anchor could even make it less efficient, since the regex parser now needs to search the entire URL. With a ^ anchor it can fail on the first character mismatch, which is more efficient.

To improve efficiency then reduce the number of rewrite directives to as few as possible and move them to your server config and disable .htaccess.

2
  • I did not even need to read your answer to up-vote. I read it afterwards. ;-) Nicely done as always!
    – closetnoc
    Commented Jan 17, 2016 at 19:35
  • Well I learned that now after literally running a test on a page not affected by mod_rewrite and the TTFB has not improved much. I must either have too many modules loaded in or my server just isn't fast enough. Thanks again Commented Jan 17, 2016 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.