Search code examples
.htaccesswebhttp-status-code-404http-status-code-301

htaccess 301 vs redirecting 404


I'm migrating my site from Drupal, and in the process all of the urls will change to a new format. I've been looking at a 301 redirect through the .htaccess file to get the job done. There's ~150 pages in total, each with about 2 ways to access it (a user friendly "blog/foo", and the default "node/123" - I'm not including the "unclean" version where each url is prepended with "?q="). They don't really follow a format, so I can't have one rule to catch them all (and in the darkness bind them).

When reading up one .htaccess, it seems that for every url request, the .htaccess is accessed, and all of the rules are run through in order, to see if one matches. Meaning that for my ~150 pages, around 300 rules will be tested in the worst case (the user inputting a url that doesn't need to be redirected). Is that right?

What are the pros and cons of redirecting using a 301 vs letting all the old urls hit a custom 404 page, which would try and do the redirect there? So instead of the server running 300 rules for every url, it only runs them if the 404 page is accessed.

Is the general rule to use a 301 in order to pass pagerank or whatever to the new url?


Solution

  • When reading up one .htaccess, it seems that for every url request, the .htaccess is accessed, and all of the rules are run through in order, to see if one matches. Meaning that for my ~150 pages, around 300 rules will be tested in the worst case (the user inputting a url that doesn't need to be redirected). Is that right?

    The .htaccess file is cached and any directives in them are cached. It's definitely faster having mod_rewrite rules in the server or vhost config, but I've never noticed any significant slowdown when I've got tens of thousands of rules in an htaccess file. In your case where there's probably going to be some 150 patterns that get applied, it's probably not going to be noticeable.

    Other options here is doing it through a script or through a plugin in your CMS, and it's debatable whether that's any faster than dynamic rules within the webserver itself.

    Another option is using a RewriteMap, which is something that you can only setup from your server/vhost config. It lets you create an external mapping (via text file, a program, a database query, etc) of matches that you can use from an htaccess file using a single rule.

    What are the pros and cons of redirecting using a 301 vs letting all the old urls hit a custom 404 page, which would try and do the redirect there? So instead of the server running 300 rules for every url, it only runs them if the 404 page is accessed.

    Is the general rule to use a 301 in order to pass pagerank or whatever to the new url?

    Yes, if you 404, whatever meta data that was associated with your old link is gone. If you 301 redirect it to the new location, you take with you whatever meta data was associated with your old link. If you don't care about SEO or meta data, then letting those links 404 is definitely easier.