I want to redirect every URL which ends with .css to a specific CSS file. This is because we are using Mouseflow for analysis and sometimes we generate a new CSS file.

Mouseflow is then trying to load the "old" CSS file from the time it is recorded, but this file does not exist anymore... so the new file hast to be loaded.

Is there something like www.example.com/*.css --> www.example.com/374893.css?

  • I assume you meant "rewrite" (as in an internal URL rewrite), as opposed to an external "redirect"? – MrWhite Nov 18 '19 at 11:12

Try something like the following (using mod_rewrite) near the top of your root .htaccess file:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/374893\.css$
RewriteRule \.css$ 374893.css [L]

The above rewrites all requests that end in .css (except for /374893.css) to 374893.css.

UPDATE: no the css is not in the root. But do I need to name the full path? I though just matching the filename is enough

If you are editing the root .htaccess file and the CSS files are located in a /styles subdirectory then you would need to modify your directives like this:

RewriteCond %{REQUEST_URI} !/374893\.css$
RewriteRule \.css$ styles/374893.css [L]

This assumes the RewriteBase directive is either not set at all or set to / (a single slash). If it's set to a different location then you will need to prefix the RewriteRule substitution with a slash, ie. /styles/374893.css.

Alternatively, if all requests are also for the /styles subdirectory and this directory only contains .css files then it would be more efficient to instead create an additional .htaccess file at /styles/.htaccess with a single rule:

RewriteEngine On

RewriteRule !^374893\.css$ 374893.css [L]

Creating an additional .htaccess in this way avoids the directive being unnecessarily processed for all non-CSS requests.

| improve this answer | |
  • I use this now but is does not work RewriteCond %{REQUEST_URI} !^/1573833767_77422c7c50808c66cb9d2b1dde821d8b\.css$ RewriteRule \.css$ 1573833767_77422c7c50808c66cb9d2b1dde821d8b.css [L] – user106349 Nov 18 '19 at 16:32
  • In what way does it "not work"? Do you get an error? Undesirable output? Is your CSS file receiving the request? What response do you get? Is seemingly nothing happening? Do you have other directives in your .htaccess file? Do you have any other .htaccess files? (It's a bit unusual that you have CSS files in the document root?) – MrWhite Nov 18 '19 at 16:44
  • no the css is not in the root. But do I need to name the full path? I though just matching the filename is enough – user106349 Nov 19 '19 at 14:56
  • 1
    "do I need to name the full path?" - it depends, but probably yes. Where is this .htaccess file? What is the URL of the requested .css file? And what is the filesystem path of the .css file you want to "redirect" to? – MrWhite Nov 19 '19 at 15:07
  • I've updated my answer. If you add more details about your filesystem then I can be more specific in my answer. Also note that directives in .htaccess don't work in isolation - the order matters. – MrWhite Nov 21 '19 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.