Redirect all media requests (css, js and images) with .htaccess

Asked
Active3 hr before
Viewed126 times

9 Answers

requestsredirect
90%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , How can an NPC replace some pages of a book with different pages, without leaving a trace of manipulation? , Reconnecting with a previous professor then asking right away for a reference letter , By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

I'm building an MVC application and I am having some problems with the path of css files, js and images in html ... I was given a solution to use the <base> tag to define the path that all html files follow, ex : <base href="http://localhost/mvc/>, only back and forth had some problems installing javascript plugins, since when I put, for example, href="#" that clicking instead not to do any action, the link took me to the URL that was defined with the <base> tag would be http://localhost/mvc/#, I wonder how I can redirect all "requests" of those files to a subfolder , ex:

<link rel="stylesheet" href="main.css">
<img src="images/img2.jpg" />
load more v
88%

I am building an MVC application and I was having some problems with the path of the css, js and images files in the html ... I was given a solution to use the <base> tag to set the path that all html files would follow, ex: <base href="#">#

Did you try something like this? I think it solves your problem.

    RewriteCond % {
       REQUEST_FILENAME
    }!-f
    RewriteCond % {
       REQUEST_FILENAME
    }!-d
    RewriteCond % {
       DOCUMENT_ROOT
    }
    /public/$0 - f
    RewriteRule ^ . + \.(jpg | jpeg | gif | png | ico | css | js | swf) $ / public / $0[L]
72%

Apache .htaccess files allow users to configure directories of the web server they control without modifying the main configuration file.,Block access to hidden files and directories,To mitigate the possibility of these attacks, you should use the crossorigin attribute in the images you request and the code snippet below in your .htaccess to set the CORS header from the server.,If you have access to the main server configuration file, use the TraceEnable directive instead.

<IfModule mod_alias.c>
   # Redirect to a URL on a different host
   Redirect "/service" "http://foo2.example.com/service"

   # Redirect to a URL on the same host
   Redirect "/one" "/two"

   # Equivalent redirect to URL on the same host
   Redirect temp "/one" "/two"

   # Permanent redirect to a URL on the same host
   Redirect permanent "/three" "/four"

   # Redirect to an external URL
   # Using regular expressions and RedirectMatch
   RedirectMatch "^/oldfile\.html/?$" "http://example.com/newfile.php"
</IfModule>
load more v
65%

I think that’s totally legit. A publication-style site probably doesn’t need every single image they’ve ever uploaded as part of their main repo.,Here’s his Apache rewrite rules:,I found my answer in Apache URL rewrite rules. When the Apache program handles incoming web page requests, rewrite rules allow it to change URLs matching certain patterns — for example, they can turn requests for the ‘files’ directory on your local machine into requests for remote URLs on the production server.,I faced the same problem couple of years back, and built this litte chrome plugin. works with all kinds of web servers :)

Here’s his Apache rewrite rules:

RewriteEngine on
# Force image styles that have local files that exist to be generated.
RewriteCond % {
      REQUEST_URI
   } ^ /sites/([ ^ \/]*)/files / styles / [ ^ \/]*/public / ((.*)) $
            RewriteCond % {
               DOCUMENT_ROOT
            }
            /sites/ % 1 / files / % 2 - f
            RewriteRule ^ (.*) $ $1[QSA, L]
            # Otherwise, send anything
            else that 's in the files directory to the production server.
            RewriteCond % {
               REQUEST_URI
            } ^ /sites/ [ ^ \/]*/files / .*$
               RewriteCond % {
                  REQUEST_URI
               }! ^ /sites/ [ ^ \/]*/files / css / .*$
                  RewriteCond % {
                     REQUEST_URI
                  }! ^ /sites/ [ ^ \/]*/files / js / .*$
                     RewriteCond % {
                        REQUEST_FILENAME
                     }!-f
                     RewriteCond % {
                        REQUEST_FILENAME
                     }!-d
                     RewriteRule ^ (.*) $ http: //www.example.com/$1 [QSA,L]

He was able to paste those rules into a little box in MAMP that handles it. I’m running WordPress locally through a little Docker setup, so I went for just editing my `.htaccess` file directly, and kinda simplified it for my needs:

RewriteCond % {
      REQUEST_URI
   } ^ /wp-content/uploads / [ ^ \/]*/.*$
      RewriteRule ^ (.*) $ https: //css-tricks.com/$1 [QSA,L]
load more v
75%

Once in place, this rule set redirects all requests from old-domain.com to new-domain.com.,Alternately, if you want to redirect all requests from the old domain to the same URL on the new domain (e.g., the homepage), we can use RedirectMatch, like:,More fun with RewriteCond & RewriteRule,Here are some further examples of redirecting and rewriting with Apache’s rewrite module, mod_rewrite.

It is an excellent idea to consistenly and logically comment your .htaccess files. Any line in an .htaccess file that begins with the pound sign ( # ) tells the server to ignore it. Multiple lines require multiple pounds and use letters/numbers/dash/underscore only:

# this is a comment
# each line must have its own pound sign
# use only alphanumeric characters along with dashes - and underscores _
load more v
40%

How to Edit .htaccess WordPress File?,Default WordPress htaccess File,WordPress .htaccess Redirects 301 (Permanent) Redirect 302 (Temporary) Redirect Force URL to www Force URL to non-www Force HTTPs Force HTTP Redirect Domain To Sub-Directory Redirect a URL ,Default WordPress .htaccess file

Here is how the default .htaccess file for WordPress looks like:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L
</IfModule>
# END WordPress
load more v
22%

Serve All Requests With One PHP File,WordPress .htaccess for permalinks,Redirect hotlinkers and show a different image,Disable Image Hotlinking

RewriteCond % {
   REQUEST_FILENAME
}!-f
RewriteCond % {
   REQUEST_FILENAME
}!-d
RewriteRule ^ ([ ^ ? ] * ) $ / index.php[NC, L, QSA]

(This is the only rule in this section that includes the RewriteEngine on rule)

# BEGIN WordPress
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteEngine on
RewriteCond % {
   HTTP_HOST
} ^ example\.com[NC]
RewriteRule ^ (.*) $ https: //www.example.com/$1 [L,R=301,NC]
RewriteCond % {
   HTTP_HOST
}! ^ $
RewriteCond % {
   HTTP_HOST
}! ^ www\.[NC]
RewriteCond % {
   HTTPS
}
s ^ on(s) |
   RewriteRule ^ http % 1: //www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

It's still open for debate whether www or non-www is the master race, so if you happen to be a fan or bare domains, here you go:

RewriteEngine on
RewriteCond % {
   HTTP_HOST
} ^ www\.example\.com[NC]
RewriteRule ^ (.*) $ https: //example.com/$1 [L,R=301]
RewriteEngine on
RewriteCond % {
   HTTP_HOST
} ^ www\.
RewriteCond % {
   HTTPS
}
s ^ on(s) | off
RewriteCond http % 1: //%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
   RewriteRule ^ % 1 % 3 % {
      REQUEST_URI
   } [R = 301, L]

Use this to redirect non HTTPS requests to a HTTPS request. I.e. if you go to https://example.com/ it will redirect to https://example.com.

RewriteEngine on
RewriteCond % {
   HTTPS
}!on
RewriteRule(.*) https: //%{HTTP_HOST}%{REQUEST_URI}

Useful if you have a proxy in front of your server performing TLS termination.

RewriteCond % {
   HTTP: X - Forwarded - Proto
}!https
RewriteRule(.*) https: //%{HTTP_HOST}%{REQUEST_URI}

Use the follow .htaccess rule to redirect any urls to the same url (but with a trailing slash) for any requests that do not end with a trailing slash. I.e. redirect from https://example.com/your-page to https://example.com/your-page/

RewriteCond % {
   REQUEST_URI
}
/+[^\.]+$
RewriteRule ^ (. + [ ^ /])$ %{REQUEST_URI}/ [R = 301, L]

Use this to remove any trailing slash (it will 301 redirect to the non trailing slash url)

RewriteCond % {
   REQUEST_FILENAME
}!-d
RewriteRule ^ (.*) / $ / $1[R = 301, L]

Redirect a single URL to a new location

Redirect 301 / oldpage.html https: //www.yoursite.com/newpage.html
   Redirect 301 / oldpage2.html https: //www.yoursite.com/folder/
RewriteEngine On
RewriteRule ^ source - directory / (.*) target - directory / $1
RewriteEngine On
RewriteRule ^ $ index.fcgi / [QSA, L]
RewriteCond % {
   REQUEST_FILENAME
}!-f
RewriteCond % {
   REQUEST_FILENAME
}!-d
RewriteRule ^ (.*) $ index.fcgi / $1[QSA, L]

Use the following .htaccess rule to redirect an entire site to a new location/domain

Redirect 301 / https: //newsite.com/

This snippet lets you use "clean URLs" -- those without a PHP extension, e.g. example.com/users instead of example.com/users.php.

RewriteEngine On
RewriteCond % {
   SCRIPT_FILENAME
}!-d
RewriteRule ^ ([ ^ .] + ) $ $1.php[NC, L]
load more v
60%

Let us know how you built your website and we may be able to assist you further.,Sounds like some of your resources are loading over HTTP. Make sure your CSS, images, and JS all load over HTTPS. Depending on how your website is built this can be accomplished many ways. If it’s a WordPress website, there’s a plugin to force HTTPS which works well.,I’m new to this website building so please bear with me.  Where do I go in my cpanel to add this code?,This will tell you, what the reason is, that the padlock isn’t on your website.

WARNING: If you have existing code in your .htaccess, add this above where there are already rules with a similar starting prefix.

RewriteEngine On
RewriteCond % {
   HTTPS
}!on
RewriteCond % {
   REQUEST_URI
}! ^ /[0-9]+\..+\.cpaneldcv$ 
RewriteCond % {
   REQUEST_URI
}! ^ /\.well-known/pki - validation / [A - F0 - 9] {
   32
}\.txt( ? : \Comodo\ DCV) ? $
RewriteRule(.*) https: //%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

WARNING: If you have existing code in your .htaccess, add this above where there are already rules with a similar starting prefix.

RewriteCond % {
   REQUEST_URI
}! ^ /[0-9]+\..+\.cpaneldcv$
RewriteCond % {
   REQUEST_URI
}! ^ /\.well-known/pki - validation / [A - F0 - 9] {
   32
}\.txt( ? : \Comodo\ DCV) ? $
RewriteEngine On
RewriteCond % {
   HTTP_HOST
} ^ example\.com[NC]
RewriteCond % {
   SERVER_PORT
}
80
RewriteRule ^ (.*) $ https: //www.example.com/$1 [R=301,L]

If this doesn’t work, try removing the first two lines.

RewriteEngine On
RewriteCond % {
   HTTP_HOST
} ^ example\.com[NC]
RewriteCond % {
   SERVER_PORT
}
80
RewriteRule ^ (.*) $ https: //www.example.com/$1 [R=301,L]

WARNING: If you have existing code in your .htaccess, add this above where there are already rules with a similar starting prefix.

RewriteCond % {
   REQUEST_URI
}! ^ /[0-9]+\..+\.cpaneldcv$
RewriteCond % {
   REQUEST_URI
}! ^ /\.well-known/pki - validation / [A - F0 - 9] {
   32
}\.txt( ? : \Comodo\ DCV) ? $
RewriteEngine On
RewriteCond % {
   SERVER_PORT
}
80
RewriteCond % {
   REQUEST_URI
}
folder
RewriteRule ^ (.*) $ https: //www.example.com/folder/$1 [R=301,L]
load more v
48%

Redirect any request for a non-existent page an alternate catch-all location:,Block access to files in a directory if they’re being called directly or from a site other than yours: (useful for js, css, or image directories),Re-enable Apache’s directory listing feature:,If you have trouble accessing this page or need other technical assistance, contact servicedesk@osu.edu

Force everyone to use encrypted HTTPS & remove www. from the start of the URL:

 RewriteCond % {
    SERVER_NAME
 } ^ www\.[NC, OR]
 RewriteCond % {
    HTTPS
 } ^ off$[NC]
 RewriteCond % {
    SERVER_NAME
 } ^ (www\.) ? (.*)[NC]
 RewriteRule ^ /?(.*) https:/ / % 2 / $1[L, R = 301, NE, QSA]
load more v

Other "requests-redirect" queries related to "Redirect all media requests (css, js and images) with .htaccess"