htaccess重写缓慢...更好的方法吗?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了htaccess重写缓慢...更好的方法吗?相关的知识,希望对你有一定的参考价值。

我需要理清我的htaccess规则,因为我认为这是许多缓慢请求的原因......

我做了以下工作,但不确定是否有一个更好和有效的方式,而不是我现在正在做的方式...我之前真的不能改变结构,所以我必须这样做,改变我的方式将来结构更好。

我希望它是动态的,无需更改现有的URL,很多人都知道并且索引已经很久以前...而且,我不想永久重定向....

我的文件结构,(都在同一个文件夹中)

# If requested resource exists as a file or directory go to it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [L]

RewriteRule ^sitemap.xml$ /sitemap.php [L]

# i dynamically change the page to AMP if /amp/pagename.php is accessed....
RewriteRule ^amp/(.*) /$1 [NC,QSA,L]
#i have added this one rule again as  the amp page for the home page is just /amp and the rule above with the trailing slash, didnt execute...
RewriteRule ^amp(.*) /$1 [NC,QSA,L]


RewriteRule ^car-price-(.*)-([0-9]+).php$ _car-price-mn-yr.php?gpimnt=$1&gpiyr=$2 [QSA,L]
    #car-price-january-2018.php   <- multiple urls: all months, years from 2009 to now... 

RewriteRule ^car-price-([0-9]+).php$ _car-price-yr.php?gpiyr=$1 [QSA,L]
    #car-price-2009.php <- yearly summary, many urls: 2009 till now.

RewriteRule ^car-price-([A-Za-z-]+).php$ _car-price-wildcard.php?_gpwilcvar=$1 [NC,QSA,L]
    #car-price-london.php <- similar to the above, making it confusing.... many cities...car-price-scotland.php  etc

RewriteRule ^car-price-([w-]+).php$ _car-price-wildcard.php?_gpwilcvar=$1 [NC,QSA,L]
    #gbp-rate-today-usa.php  <- i have different urls for e.g. cad.rate-today-usa.php, etc...

RewriteRule ^([w-]+).php$ _car-price-wildcard.php?_gpwilcvar=$1 [NC,QSA,L]
    #why-do-diesel-cars-cost-more.php  <-i have many articles that does not have a prefix or some sort to make this easier..

这些规则使我的网站加载时间延长了4秒....是否有更好的规则可以提供帮助,或者这是唯一的方法吗?

MOD REWRITE日志:

[Sun Mar 04 22:21:55.879811 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] strip per-dir prefix: /media/sf_repo/carPriceusa.comNEW/car-price-kolkata.php -> car-price-kolkata.php, referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.879861 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] applying pattern '(.*)' to uri 'car-price-kolkata.php', referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.879992 2018] [rewrite:trace4] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] RewriteCond: input='/media/sf_repo/carPriceusa.comNEW/car-price-kolkata.php' pattern='-f' => not-matched, referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880117 2018] [rewrite:trace4] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] RewriteCond: input='/media/sf_repo/carPriceusa.comNEW/car-price-kolkata.php' pattern='-d' => not-matched, referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880128 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] strip per-dir prefix: /media/sf_repo/carPriceusa.comNEW/car-price-kolkata.php -> car-price-kolkata.php, referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880135 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] applying pattern '^sitemap\.xml$' to uri 'car-price-kolkata.php', referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880144 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] strip per-dir prefix: /media/sf_repo/carPriceusa.comNEW/car-price-kolkata.php -> car-price-kolkata.php, referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880151 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] applying pattern '^amp/(.*)' to uri 'car-price-kolkata.php', referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880159 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] strip per-dir prefix: /media/sf_repo/carPriceusa.comNEW/car-price-kolkata.php -> car-price-kolkata.php, referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880166 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] applying pattern '^amp(.*)' to uri 'car-price-kolkata.php', referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880178 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] strip per-dir prefix: /media/sf_repo/carPriceusa.comNEW/car-price-kolkata.php -> car-price-kolkata.php, referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880195 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] applying pattern '^car-price-(.*)-([0-9]+).php$' to uri 'car-price-kolkata.php', referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880204 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] strip per-dir prefix: /media/sf_repo/carPriceusa.comNEW/car-price-kolkata.php -> car-price-kolkata.php, referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880211 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] applying pattern '^car-price-([0-9]+).php$' to uri 'car-price-kolkata.php', referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880219 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] strip per-dir prefix: /media/sf_repo/carPriceusa.comNEW/car-price-kolkata.php -> car-price-kolkata.php, referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880226 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] applying pattern '^car-price-([A-Za-z-]+).php$' to uri 'car-price-kolkata.php', referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880235 2018] [rewrite:trace2] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] rewrite 'car-price-kolkata.php' -> '_car-price-wildcard.php?_gpwilcvar=kolkata', referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880243 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] split uri=_car-price-wildcard.php?_gpwilcvar=kolkata -> uri=_car-price-wildcard.php, args=_gpwilcvar=kolkata, referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880250 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] add per-dir prefix: _car-price-wildcard.php -> /media/sf_repo/carPriceusa.comNEW/_car-price-wildcard.php, referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880259 2018] [rewrite:trace2] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] strip document_root prefix: /media/sf_repo/carPriceusa.comNEW/_car-price-wildcard.php -> /_car-price-wildcard.php, referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880266 2018] [rewrite:trace1] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c37370a0/initial] [perdir /media/sf_repo/carPriceusa.comNEW/] internal redirect with /_car-price-wildcard.php [INTERNAL REDIRECT], referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880509 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c372d028/initial/redir#1] [perdir /media/sf_repo/carPriceusa.comNEW/] strip per-dir prefix: /media/sf_repo/carPriceusa.comNEW/_car-price-wildcard.php -> _car-price-wildcard.php, referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880519 2018] [rewrite:trace3] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c372d028/initial/redir#1] [perdir /media/sf_repo/carPriceusa.comNEW/] applying pattern '(.*)' to uri '_car-price-wildcard.php', referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880744 2018] [rewrite:trace4] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c372d028/initial/redir#1] [perdir /media/sf_repo/carPriceusa.comNEW/] RewriteCond: input='/media/sf_repo/carPriceusa.comNEW/_car-price-wildcard.php' pattern='-f' => matched, referer: http://carpriceusav2.local/silver-price-usa.php
[Sun Mar 04 22:21:55.880758 2018] [rewrite:trace1] [pid 11803] mod_rewrite.c(476): [client 192.168.0.17:51780] 192.168.0.17 - - [carpriceusav2.local/sid#7fe0c37f2140][rid#7fe0c372d028/initial/redir#1] [perdir /media/sf_repo/carPriceusa.comNEW/] pass through /media/sf_repo/carPriceusa.comNEW/_car-price-wildcard.php, referer: http://carpriceusav2.local/silver-price-usa.php
答案

结合/amp规则:

RewriteRule ^amp(/.+)? /$1 [NC,QSA,L]

作为一般的经验法则,避免使用.*匹配,因为*是贪婪的并且创造更大的回溯。

接下来,您可以在单个PHP文件中执行此操作,而不是使用单独的PHP脚本接受可选的gpimnt参数。如果不存在相同的值,则仅继续接收gpiyr值。这可以再次与条件gpwilcvar参数等组合,并减少您的URL匹配的模式总数。 PHP中的单个defined()(或isset())调用比模式匹配更快。

以上是关于htaccess重写缓慢...更好的方法吗?的主要内容,如果未能解决你的问题,请参考以下文章

apache .htaccess 重写 - 我可以把它移到 httpd.conf

我可以编辑我的 .htaccess 来编写一些 WordPress URL(自定义重写)吗?

用 .htaccess 重写不起作用

PHP 内置服务器和 .htaccess mod 重写

htaccess mod 用斜杠将 $_GET 重写为变量

我应该将移动检测放在 .htaccess 或 php 中的哪个位置?