Reputation: 710
We need to 301 redirect ~ 8000 URLS from an old website to a new website. Unfortunately there is no relation between URL's as seen below:
E.g. http://www.URL1.com/content/default.asp?var_a=s2&var_b=8443&back=home TO http://URL2.co.uk/news/article/news-title-goes-here/
We can either import all the redirection rules into an htaccess file or we can set up a mysql database and PHP script to perform a lookup and then redirect.
Given the number of entries which is the best route to go down from a performance perspective?
If htaccess would hold up to the task what is the best method of writing the redirect?
Redirect 301 /content/default.asp?var_a=s2&var_b=8443&back=home http://URL2.co.uk/news/article/news-title-goes-here/
OR
RewriteCond %{QUERY_STRING} ^var_a=s2$
RewriteCond %{QUERY_STRING} ^var_b=8443$
RewriteRule ^/content/default.asp$ http://URL2.com/news/article/news-title-goes-here/? [L,R=301]
Many thanks
Upvotes: 2
Views: 1775
Reputation: 819
Generally htaccess files will be faster with a small number of rewrites/redirects.
Regarding Redirects:
8,000 is not a terribly large number of redirects. Depending on how Apache is running for you, doing this with an htaccess file may end up adding extra memory to each Apache thread since each thread may need to hold the entire list in memory, but this won't be more than a few megs of RAM at most. Memory is cheap nowadays. Do you have severe memory constraints on your server - meaning are you close to paging?
The CPU hit for a straight lookup of an in memory hash from Apache will be negligible (assuming the whole thing is stored in memory - so basically assuming you answered "no" to having severe memory constraints on your server).
The delay for having to parse a PHP page for each file, and then do a MySQL lookup, and then return the results to PHP will definitely be a much bigger hit than just using Apache. Remember, you'll need to load the PHP interpreter, connect to the MySQL server, parse the results, and build a page from it. This is a pretty complex process compared to a simple redirect.
Regarding Rewrites:
Are you already loading ModRewrite? It's a pretty damn efficient beast. Assuming you aren't doing rewrites more complicated than those above, it's definitely safe to use it.
If you are not already loading it, and you can easily make a simple list of rewrites, then you should ask yourself: Is your server more CPU limited or more RAM limited. If more RAM limited, use mod_rewrite as your rules will likely be orders of magnitude smaller than a list of redirects.
All that said, the main thing you need to determine is if you really need to run this on every single server request.
For example:
If you know you need to run this on every request in the /content/ directory on your server, then throw it in a .htaccess file in /content/
However if you only need to run it on a request to /content/default.asp?var_a=blah and there are lots of other requests to /content/default.asp that don't need the redirect, then make it a PHP lookup.
Upvotes: 3