Mobile app version of vmapp.org
Login or Join
Deb1703797

: Can rewriterules work without carets or dollar signs? 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

@Deb1703797

Posted in: #ModRewrite #Performance #UrlRewriting

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= [L]
RewriteRule ^afolder2/subfolder2/(.*)$ /internal.php?Q2= [L]
....
RewriteRule ^something/else/(.*)$ /internal.php?Qn= [L]


As you can see, I'm remapping example.com/whatever/whatever/value to 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 example.com/anything to 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.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Deb1703797

1 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

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= [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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme