themistral Posted April 23, 2010 Share Posted April 23, 2010 Hi guys, I use mod rewrite to create seo friendly urls, but they seem to be massively slowing the server down. If I ignore a file in the rewrite rules, that loads almost immediately. Currently the homepage is taking over 5 seconds to load which we all know is bad news. Can anyone offer any insight into why the following rules are slow - can I improve them at all? Thanks in advance. Options +FollowSymLinks RewriteEngine on RewriteBase / ################################################################################################ ######################## Redirect non-www to www ######################## ################################################################################################ RewriteCond %{HTTP_HOST} ^domain\.co\.uk [NC] RewriteRule ^(.*) http://www.domain.co.uk/ [R,L] ######################################## 301 Redirects ######################################### RewriteRule ^amateur-dramatics-groups/dramatic/list.html$ http://www.domain.co.uk/amateur-dramatics-groups/dramatic-theatre/list.html [L,R=301] RewriteRule ^amateur-dramatics-groups/general/list.html$ http://www.domain.co.uk/amateur-dramatics-groups/general-theatre/list.html [L,R=301] RewriteRule ^amateur-dramatics-groups/kids/list.html$ http://www.domain.co.uk/amateur-dramatics-groups/kids-theatre/list.html [L,R=301] RewriteRule ^amateur-dramatics-groups/musical/list.html$ http://www.domain.co.uk/amateur-dramatics-groups/musical-theatre/list.html [L,R=301] ################################################################################################# RewriteCond {REQUEST_URI} !^server-test.php [NC] RewriteRule ^$ list.html [L] RewriteRule ^admin/(.*).html$ admin/admin-template.php?page=$1 [L] RewriteRule ^error-(.*)/(.*).html$ template.php?error=$1&page=$2 [L] RewriteRule ^amdram-search/(.*)/search.html$ template.php?search=$1&page=2$ [L] RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*).html$ template.php?cat1=$1&cat2=$2&cat3=$3&cat4=$4&cat5=$5&page=$6 [L] RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*).html$ template.php?cat1=$1&cat2=$2&cat3=$3&cat4=$4&page=$5 [L] RewriteRule ^(.*)/(.*)/(.*)/(.*).html$ template.php?cat1=$1&cat2=$2&cat3=$3&page=$4 [L] RewriteRule ^(.*)/(.*)/(.*).html$ template.php?cat1=$1&cat2=$2&page=$3 [L] RewriteRule ^(.*)/(.*).html$ template.php?cat1=$1&page=$2 [L] RewriteRule ^(.*).html$ template.php?page=$1 [L] ErrorDocument 401 /error-401/restricted-access.html ErrorDocument 403 /error-403/page-not-found.html ErrorDocument 404 /error-404/page-not-found.html Redirect 301 /amateur%20dramatics%20groups/index.html http://www.domain.co.uk/amateur-dramatics-groups/list.html Redirect 301 /organisations/index.html.html http://www.domain.co.uk/organisations/list.html Redirect 301 /sound/index.html.html http://www.domain.co.uk/sound/list.html Quote Link to comment https://forums.phpfreaks.com/topic/199484-rewrite-rules-slowing-loading/ Share on other sites More sharing options...
cags Posted April 23, 2010 Share Posted April 23, 2010 I don't see how it could cause a delay of that much. Are you saying that if you go directly to the URL using it's 'ugly' path... template.php?cat1=$1&cat2=$2&cat3=$3&cat4=$4&page=$5 ...it loads instantly? A couple of things I would mention. For the 301 redirects at the start, use the redirect directive rather than RewriteRule (like you have at the bottom of the script). The last period in the path should be escaped so that it is '\.html' rather than just '.html'. It would probably be a good idea to replace the .* with [^/]+, this has the most potentially for speeding up the redirect as it's a more efficient Regex. Quote Link to comment https://forums.phpfreaks.com/topic/199484-rewrite-rules-slowing-loading/#findComment-1047002 Share on other sites More sharing options...
themistral Posted April 23, 2010 Author Share Posted April 23, 2010 Thanks cags. Yep if I go to template.php?cat1=$1&cat2=$2&cat3=$3&cat4=$4&page=$5 then it loads pretty much instantly. I have made the changes you suggested and can't see any real difference in the load speed. I've checked with my hosts and they are insistant the server cpu is running at around 30% so there it's not the server slowing it down. They think it is to do with the number of redirects in the htaccess... I'm still learning htaccess so your thoughts on this would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/199484-rewrite-rules-slowing-loading/#findComment-1047028 Share on other sites More sharing options...
cags Posted April 23, 2010 Share Posted April 23, 2010 That is not an overly large number or redirects and the use of the [L] flag means that it will only ever run through the file once so I'm not sure what could slow it down that much. Another couple of things I just spotted... RewriteCond {REQUEST_URI} !^server-test.php [NC] RewriteRule ^$ list.html [L] I'm not entirely sure why you have this. The RewriteCond check the requested URI doesn't begin with server-test.php, but the RewriteRule will only forward an empty request URI anyway, thus making the RewriteCond a little pointless. Also the objective of the RewriteRule seems to be to redirect domain.com to domain.com/list.html, which would probably be better done with the DirectoryIndex directive. RewriteCond %{HTTP_HOST} ^domain\.co\.uk [NC] RewriteRule ^(.*) http://www.domain.co.uk/ [R,L] This will forward any request that doesn't have www in it to your site root, is this the objective? I'd have thought you wanted to maintain the page, but change subdomain. If that is the case then I think you need to carry the requested URI over. You also probably don't want the L flag otherwise they won't be directed to the appropriate page. RewriteCond %{HTTP_HOST} ^domain\.co\.uk [NC] RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R] Quote Link to comment https://forums.phpfreaks.com/topic/199484-rewrite-rules-slowing-loading/#findComment-1047033 Share on other sites More sharing options...
themistral Posted April 23, 2010 Author Share Posted April 23, 2010 Thanks for the spots! I think I will go back to my hosts and ask THEM to tell me what they think is wrong with it. Much appreciated, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/199484-rewrite-rules-slowing-loading/#findComment-1047036 Share on other sites More sharing options...
cags Posted April 23, 2010 Share Posted April 23, 2010 Unfortunately I doubt you'll host will prove much use, they generally don't in this sort of situation. I don't think you mentioned exactly where you have this code, but I assume it's probably in a .htaccess file. If you have access you can improve performance by moving the code into your httpd.conf file as this would make it per-server rather than per-directory which gives a performance boost. Also it's worth checking that RewriteLog is disabled as this will give a performance hit. Finally reversing the order of your per-directory patterns may help minutely as a user would presumably access the short URL's more often. Given the patterns you had initially this would work since the .* will actually match the forward slashes, but using the [^/]+ syntax I suggested this is no longer an issue. That's me out of ideas . Quote Link to comment https://forums.phpfreaks.com/topic/199484-rewrite-rules-slowing-loading/#findComment-1047050 Share on other sites More sharing options...
themistral Posted April 25, 2010 Author Share Posted April 25, 2010 Thanks for your help cags. I've spent a bit of time looking at the site and running a few tools has shown that it's the actual connecting to the site that is slow. After a bit of testing by removing various things on the homepage, I've concluded there are too many database calls, and this is slowing the load time. So, thanks again for the help!! Quote Link to comment https://forums.phpfreaks.com/topic/199484-rewrite-rules-slowing-loading/#findComment-1047941 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.