简体   繁体   中英

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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM