erax
erax

Reputation: 111

Apache2 mod_rewrite rules

How do i rewrite static files request like http://example.com/assets/img/logo.png to http://assets.example.com/img/logo.png

main site rewrite rules

    ServerName    example.com
    ServerAlias   example.com
    DocumentRoot /var/www/html/example.com/public/

   <Directory  /var/www/html/example.com/public/ >
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      allow from all
    </Directory>

RewriteEngine on
RewriteCond %{HTTP_HOST} !assets\.site\.com
RewriteCond %{REQUEST_URI} \.(png|gif|ico|css|js|tiff|woff|woff2|ogg|mp3)$ [NC]
RewriteRule ^(.*) http://assets.example.com$1 [NC,L]

sub-domain for assets is working properly

virtual host config for assets.example.com

    ServerName     assets.example.com
    ServerAlias   assets.example.com
    DocumentRoot /var/www/html/example.com/public/assets/

   <Directory  /var/www/html/example.com/public/assets/ >
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      allow from all
    </Directory>

http://assets.example.com/img/logo.png gives a 404 but if i create a /var/www/html/example.com/public/assets/assets/ then http://assets.example.com/img/logo.png reutns 200

Upvotes: 0

Views: 47

Answers (1)

Charles
Charles

Reputation: 1122

Per your comment, you just need to change the what you're capturing in the regex used by the RewriteRule. Right now $1 is the value of what's in (.*). You can remove "assets" from that by adding it to the path prior to your capture group.

Change:

RewriteRule ^(.*) http://assets.example.com$1 [NC,L]

To:

RewriteRule ^assets/(.*) http://assets.example.com/$1 [NC,L]

Upvotes: 1

Related Questions