Search code examples
apache.htaccessmod-rewritedrupalacquia

Mod rewrite condition for subdomain with pass through


I wanted to run this by you because I'm attempting to perfect an Apache mod rewrite rule, which works, but I'm going insane because my file paths are breaking.

Basically, I've set up a mod rewrite rule to redirect a subdomain, to a subdirectory in my docroot which contains a static index.html file. Existing rules are listed as follows:

  RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$
  RewriteCond %{REQUEST_URI} !^/subdirectory/
  RewriteRule ^(.*)$ /subdirectory/$1 [PT]

The redirect works fine. What happens though, is my images are breaking. For example, '/subdirectory/img', with the following img tag, doesn't work for me:

   <img src="img/some-image.png" />

Same thing happens when I move the image to root, and attempt to call the image directly minus the 'img/' path.

Does my redirect have anything to do with this? Or am I missing something? I was under the impression that using the Pass Through [PT] flag would mean that asset linking would work.

Any advice is greatly appreciated. Thanks for your help!

Mark.


Solution

  • Issue resolved. The rule works perfectly, it was just the positioning in the .htaccess file. I moved it directly underneath the following:

      RewriteEngine     On
    

    Images and JS assets are now loading fine. I also added a missing 'L' flag.

    Final working code:

      RewriteEngine     On
      RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$
      RewriteCond %{REQUEST_URI} !^/subdirectory/
      RewriteRule ^(.*)$ /subdirectory/$1 [PT, L]