Ignore .htaccess RewriteRules if a condition is not met

时间:2017-10-12 10:10:13

标签: php wordpress apache .htaccess mod-rewrite

I have a fairly complicated website that is made up of sub sites that work like subdirectories. It is a PHP site where php7 is running under apache 2.4 using php-fpm (centos 7). Every site on this server is a wordpress site and depending on the theme, some are rendering fully in 0.3-0.5 seconds (the performance is incredibly good). There is one ugly duckling that is not going so quick. This site is also wordpress, but there are some custom subdirectory sites built into it. None of them slow down wordpress, but the entire site is slow and i think it is down to the vhost configuration.

There are 70+ rewrite rules and i feel it might be causing some performance issues as the only difference that this vhost has compared to the others is the excessive amount of rewrite rules. There are several blocks of rules for the subdirectories that are listed like this.

RewriteRule ^reviews/scripts/(.*)$ /nbreviews/scripts/$1 [L]
RewriteRule ^reviews/sitemapindex.xml nbreviews/sitemapindex.php [L]
RewriteRule ^reviews/sitemap([^/]*)\.xml$ /nbreviews/sitemap.php?page=$1 [L]
RewriteRule ^reviews/([^/]*)/([^/]*)$ nbreviews/index.php?pagetype=$1&id=$2 [QSA,L]
RewriteRule ^reviews/?(.*)$ nbreviews/index.php?pagetype=$1 [QSA,L]
RewriteRule ^review/([^/]*)$ /nbreviews/index.php?pagetype=review&id=$1 [QSA,L]

If someone were to visit a page say https://website.com/some-random-post, the url would be compared against all 7 of those rules even though we know that because it doesn't contain reviews/ or review/ it will fail to match against all of these rules. Is there a way i can just bypass these rules if one fails. I know the if blocks exist, but they are just for checking if a module exists.

In my attempts to make things quicker, I have firstly moved all of these rules to the <Directory> block in the httpd.conf file so they are stored in memory. I was also able to remove some stuff like checking to see if the user was https and redirecting if not. Instead I setup a http vhost that does the redirect. From all of this I have seen a drop in about 50ms in the response, but it is still about 400ms slower than all other sites on the server.

Is there any logic I can add to reduce the amount of rule comparisons made without actually removing anything?

0 个答案:

没有答案