大网数据广告活动
网站运营与故障问答

Can rewriterules work without carets or dollar signs?

· 52CE 我爱测

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/(…

问题原文

原帖链接 · Mike -- No longer here · CC BY-SA 3.0

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.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-01-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。