htaccess redirect between subdomains depending on the file extension

Kenny Lajara

Situtation

I have 2 subdomains www.example.com and static.example.com and they both read the files on the same folder. So:

  1. If I go to https://static.example.com/privacy I see the same that I see on https://www.example.com/privacy
  2. If I go to https://www.example.com/src/file.js I see the same that I see on https://static.example.com/src/file.js
  3. Also, both subdomains read the same .htaccess file.

What I want to do

I want is to redirect any request of an image, video, js or css file from www to static and any other file type from static to www.

Expected Behavior

That way, if I go to https://static.example.com/privacy I should be redirected to https://www.example.com/privacy

And, if I go to https://www.example.com/src/file.js I see the same the is on https://static.example.com/src/file.js

MrWhite

You can do something like the following:

# Identify static resources - set an environment variable
SetEnvIf Request_URI "(?i)\.(jpg|png|gif|mpg|mp4|wmv|js|css)$" STATIC_RESOURCE

# Redirect static resources to static subdomain
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{ENV:STATIC_RESOURCE} =1
RewriteRule \.([\w]{2,4})$ https://static.example.com%{REQUEST_URI} [R=301,L]

# Redirect "other" requests to www
RewriteCond %{HTTP_HOST} ^static\.
RewriteCond %{ENV:STATIC_RESOURCE} ^$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

Although, I assume your site is already linking to the appropriate subdomain?

You could just force a 404 instead?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

htaccess redirect all subdomains to the same directory

Using htaccess to redirect to certain extension

How can I redirect only specific subdomains to HTTPS in htaccess?

redirect all subdomains with htaccess

How can I redirect subdomains in .htaccess to www. with generic parameters?

htaccess redirect links from multiple subdomains

Redirect all subdomains and subdirectories to index page using .htaccess

Redirect non WWW to WWW using htaccess without failing subdomains

.htaccess file for multple control pages and wildcard subdomains

Redirect directory to file .htaccess

.htaccess redirect rule for subdomains

Htaccess redirect if url contain language and file extension?

How to remove and redirect .php for subdomains in .htaccess?

How to remove multiple file extensions with Htaccess and redirect file extension URLs

redirect directories to a file with .htaccess

.htaccess non redirect subdomains

Htaccess simple extension redirect

Redirect non-www to www except subdomains by .htaccess

Apache htaccess redirect file

Redirect Rules for .htaccess File

Replace file extension with folder in htaccess redirect

Multi-site wordpress redirect between https subdomains via htaccess

htaccess rewrite and redirect php extension

http to https redirect with subdomains and www conversion via htaccess

.htaccess: how to redirect non-www to www except for subdomains

.htaccess file for WP and subdomains

htaccess to redirect subdomains to a different URL with the subdomain info

Htaccess redirect seems not working with php file extension to seo friendly (no extension) url

Redirect on condition in htaccess file