1

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.

m918273
  • 110
  • 2
  • 11

2 Answers2

0

This is because you are using a relative url path for your images. Either use an absolute path starting with a / or add the following base tag in head section of your webpage :

<base href="/">

Related : Seo Friendly Url css img js not working

Community
  • 1
  • 1
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • It's not as simple as this, I'm afraid. Though I wish it were. Regardless of what path I use, the images result in a 500 server error. I'm thinking it has more to do with the .htaccess file. subdomain.mydomain.com/img/some-image.png isn't being found. – m918273 Nov 08 '16 at 00:40
0

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]
m918273
  • 110
  • 2
  • 11
  • From the [docs](https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_pt): `The PT flag implies the L flag` – Matt Oct 03 '22 at 20:11