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?