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

时间:2018-03-04 20:17:37

标签: .htaccess mod-rewrite url-rewriting

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

我已经做了以下工作,但不确定除了我现在这样做的方式之外还有更好更有效的方式...... 我以前真的无法改变结构,所以我必须这样做,并改变我将来更好地构建它的方式。

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

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

sed -E ':A;s/([A-Z]\.)([A-Z]\.{1,})/.\1.\2/;tA;s/(\.{1,})([A-Z])(\.)/\2/g' infile

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

MOD REWRITE LOG:

# 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..

1 个答案:

答案 0 :(得分:1)

结合/amp规则:

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

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

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