The Reroute plugin is great for a handful of 301 redirects, but we have hundreds of entries and each entry has a legacy URL. Dumping this into a template as normal works fine, but thinking about if it could work with the .htaccess file directly. Has anyone tried, or would be a dangerous path?

Thinking along the lines of NSM htaccess for EE.


The more redirects you have, the better off you’re going to be having them managed outside of your .htaccess file, and in a database table.

Each redirect in your .htaccess file comes with a slight performance hit, since mod_rewrite needs to parse and evaluate each one individually, for every single request.

If you have them managed in an indexed database table, the performance hit will be trivial – just the time it takes to make a very simple DB query.

I haven’t used Reroute, but it looks like that’s just storing redirects in a DB table, so it’s probably going to be your best bet for managing hundreds of redirects. (Not sure how scalable the UI is, though.)

  • Interesting, didn't know about potential performance hits. Looking at potentially pasting the old and new urls into the db. Each new one generates a code in the db under a uid column which need to do some homework on. Jul 3 '14 at 15:25

If I understand what you're looking for, it's not so much dangerous as it is not possible. Apache looks at the filesystem and parses the .htaccess file directly, and this is done long before Apache starts to process any PHP.

A route you could take is writing the .htaccess file each time an entry is saved (or having a button that does it so you're not wasting resources). So long as PHP has the write access, this should be doable. Plus, you can still render the file via the template service.

$htaccess = craft()->templates->render('PluginHandle/path/to/template');
  • I like the idea of exporting a fresh .htaccess file rather than cached db lookup. Jul 3 '14 at 7:37
  • Thanks for this. We're a bit stuck in the middle of this in that we have a lot of legacy entries transferred with urls, but future legacy one will be added as-you-go by the client. Suspect Reroute plugin best bet here. Jul 3 '14 at 15:22

I'm not sure how safe it is but I have just imported 301 redirects into Craft-Reroute by exporting the old & new url slugs to a csv file.

I then imported that using Sequel Pro and set default values for method, date_created & date_updated. Id is auto incremental but uid isnt set unfortunately.

  • Yes, this is what I've done as well, but seems to work ok. I've set the uid so my cdv code looks like this id,"oldUrl","newUrl",method,"dateCreated","dateUpdated","uid" {% set redirects = craft.entries.order('title').limit(1000) %} {% for entry in redirects %}{% if entry.urllegacy|length %}{{loop.index}},"{{ entry.urllegacy }}","/{{ entry.uri }}","301","2014-07-24 15:00:00","2014-07-24 15:00:01","{{ entry.id }}" {% endif %}{% endfor %} Jul 24 '14 at 18:50

For a case like yours, my Retour plugin would work well, because it has a FieldType that you can attach to existing Entries, and do the redirect based on data from the entries.

So you can set up a rule in the FieldType like .*RecipeID={recipeid} as the default Legacy URL Pattern, and it'll pull the value from the {recipeid} field in each entry, and create a redirect for it to the new entry.

It also doesn't do anything unless a 404 exception actually occurs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.