简体   繁体   中英

htaccess & 301 redirects causing server slowdowns

My .htaccess file looks like this:

RewriteEngine on

RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [L,R=301]

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteRule ^resources/([^/\.]+)/?$ page.php?theme=resources&pg=$1 [L]
RewriteRule ^resources/([^/\.]+)/?/([^/\.]+)/?$ page.php?theme=resources&pg=resources&catname=$1&cat=$2 [L]
RewriteRule ^resources/([^/\.]+)/?/([^/\.]+)/?/([^/\.]+)/?$ page.php?theme=resources&catname=$1&cat=$2&pg=$3 [L]

RewriteRule ^albums/([^/\.]+)/?/([^/\.]+)/?$ page.php?theme=albums&pg=albums&albumname=$1&album=$2 [L]
RewriteRule ^albums/([^/\.]+)/?$ page.php?theme=albums&pg=albums [L]

RewriteRule ^leaving-the-site/([^/\.]+)/?$ page.php?theme=leaving-the-site&pg=leaving-the-site&q=$1 [L]

RewriteRule ^([^/\.]+)/?/([^/\.]+)/?$ page.php?theme=$1&pg=$2 [L]

RewriteRule ^([^/\.]+)/?$ page.php?theme=$1&pg=$1 [L]

Everything seems fine, but as we're relaunching the site, we have approximately 110 301 redirects we need to put in place. These are similarly formatted to the following, and added after the above rewrite rules:

RewriteRule ^about/about-the-site$ https://www.mywebsite.com/about-us? [L,NC,R=301]

My problem is that as soon as I have the htaccess file uploaded with all the 301s, it starts to slow down the site, to the point of having 60 second load times within about 10 minutes and then starts timing out.

I remove the 301 redirects, and it recovers. From my research, 301 redirects can slow down a server, but 'not significantly', at least not with less than 1000 lines.

Is there something I should be doing differently?

This is combined HTTPS/SSL & www to non-www Rewrite|Redirect htaccess code specifically for WordPress, but you can modify/customize it for your particular site. I am still looking at your other code...

Edit: Ok I looked at your other code and it is overly complex and the use of question marks in your code may be incorrect. What I recommend you do is use RewriteCond Query String matching code instead of the direction/code you are trying to use now. See this topic for example RewriteCond Query String matching htaccess code examples: Replace "index.php?option=com_k2&view=item&id=" using .htaccess and Regex

# WP REWRITE LOOP START
# Rewrite|Redirect http to https|SSL & www to non-www
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_PORT} ^80
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule ^index\.php$ - [L]

# All lines of Rewrite|Redirect code would go here

# if file or directory does not exist then Rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# WP REWRITE LOOP END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM