0

I am trying to make a couple of (I think) very basic rewrite condition / rules in .htaccess.

I placed them as the first rule after RewriteEngine on. Things seemed fine locally, and the rules appeared to work in .htaccess tester under each scenario.

However, when I pushed the code live, clean urls stopped working. Everything was only accessible as its route, i.e. /node/1234 instead of /foo/page.

Here are the rules I added:

<IfModule mod_rewrite.c>
  RewriteEngine on

  # Custom redirects
  RewriteRule ^ptistore/control/interviews(.*) http%{ENV:protossl}://%{HTTP_HOST}/nurse-interviews [NC,L,R=301]
  RewriteRule ^ptistore/control/news(.*) http%{ENV:protossl}://%{HTTP_HOST}/cpn-numbers [NC,L,R=301]

So, any hit on either URL should 301 to the provided URL. I tested them locally and they appeared to work (and not break clean URLs), but when I pushed it up to our Acquia environments, URLs were no longer rewritten.

I wound up implementing a RouteSubscriber to get around this for the moment:

/**
 * Class RedirectSubscriber
 * @package Drupal\mymodule_redirects\EventSubscriber
 */
class RedirectSubscriber implements EventSubscriberInterface {

  public function redirectIndexPHP(GetResponseEvent $event) {
    $request = $event->getRequest();
    $uri = $request->getRequestUri();

    if (preg_match('/ptistore\/control\/interviews\/.*/i', $uri)) {
      $response = new TrustedRedirectResponse('/nurse-interviews', 301);
      $event->setResponse($response);
    }

    if (preg_match('/ptistore\/control\/news\/.*/i', $uri)) {
      $response = new TrustedRedirectResponse('/news', 301);
      $event->setResponse($response);
    }
  }

  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('redirectIndexPHP', 39);
    return $events;
  }
}

Which works... but I really would rather it live in .htaccess.

Did I just place my rules too early in the file? How can I correct this?

1

No idea how acquia hosting works, but I doubt they respect your .htaccess and possibly don't even use Apache.

Maybe they have their own configuration settings for that, I know that platform.sh (which uses nginx) does.

If not, then you don't really have a choice but to do it in Drupal.

You could also try the latest redirect release which has a new submodule that supports domain and wildcard based redirects.

  • 1
    possibly don't even use Apache. I talked to an Acquia dev last weekend and he told me that they use Nginx. – No Sssweat Mar 1 '17 at 9:29

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.