4

I have a couple of legacy rewrite rule redirects that I've traditionally put in .htaccess. For instance, paths are of the format:

story/[story:title]

And paths like:

story/[story:nid]

redirect to that.

Is there any way of setting up regex-matching redirects within Drupal? I could always do it in .htaccess, but that would get overwritten on the next Drupal core update. Further, it seems that Boost is nerfing any redirect I put in .htaccess -- the above hasn't worked since I installed its redirect rules.

Thanks!

  • 1
    You can move your rewrite rules to apache configuration files. To be safe just add include your_custom_rules.conf in your main configuration file and add rules to that custom file so that it never gets overwritten. – GoodSp33d Nov 4 '12 at 18:31
  • @2-Stroker That's not a bad idea. Still wouldn't solve the issue of Boost's rewrite rules taking precedence over mine, though. – aendrew Nov 4 '12 at 18:38
2

Well if you are familiar with PHP and preg_match, you could theoretically install Drupal rules module and write custom rules using PHP to redirect pages inside Drupal without touching .htaccess file.

  • I guess that's one way to go about it. Bit heavy, though -- I don't have Rules enabled already, and to do so for simple redirects seems a bit much. – aendrew Nov 4 '12 at 17:49
  • 1
    You can use hook_init() inside modules if you are not ok with using a module just for one purpose. – GoodSp33d Nov 4 '12 at 18:28
  • @2-Stroker -- Can you explain that a bit further? – aendrew Nov 4 '12 at 18:36
  • 1
    I guess @2-Stroker meant that you write a custom module with only hook_init() and then get current url using drupal_get_destination() and then write custom PHP code to rewrite url and then use drupal_goto() to redirect page to new url. I am not sure if hook_init() is the best place to write this, but it might work as well. – Ajinkya Kulkarni Nov 4 '12 at 21:03
  • hook_boot is the better for redirects. See shurly module for an example. – AyeshK Apr 8 '13 at 18:18
1

It just dawned on me that you don't need to define Rewrite Rules in .htaccess — if the goal is to prevent Drush from nuking custom rewrites during Drupal core updates, it makes way more sense to just put them straight into the httpd.conf VirtualHost directive (If you have access to that).

See: https://serverfault.com/questions/120488/redirect-url-within-apache-virtualhost

Leaving question open in case somebody has a better solution more applicable to a broader cross-section of the community (I.e., users on shared hosting without access to .conf files).

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.