-1

Is there any reason why I'd be getting a 404 error from a CSS file with a version number on the end of it?

<link rel="stylesheet" href="styles/main.css?v=123">

My file is also named main.css?v=123.

Any help is appreciated. Thanks in advance!

realph
  • 4,481
  • 13
  • 49
  • 104
  • 2
    That's not how it's supposed to work. Don't add the parameters to the file, just call it `main.css`. – JJJ Nov 19 '15 at 20:17
  • @Juhana Huh. I'm confused...? – realph Nov 19 '15 at 20:20
  • 5
    If your file is really called "main.css?v=123", you're doing it wrong. Call it just "main.css". The whole point is that when you update the file you don't have to change its name, you just change the version number in the HTML *only*. – JJJ Nov 19 '15 at 20:22
  • @Juhana This was so my users get my most recent stylesheet instead of some cached version. Do I need the version anywhere else? In my CSS file, perhaps? – realph Nov 19 '15 at 20:24
  • 1
    I don't know how to say it any more clear. Leave the HTML as it is now, and name the CSS file "main.css". Then it'll work as you want it to. – JJJ Nov 19 '15 at 20:26
  • Thank you. Do you want to leave an answer and I'll happily accept it. – realph Nov 19 '15 at 20:27

3 Answers3

2

You should rename your file to just main.css, then in the html reference it like this: <link rel="stylesheet" href="styles/main.css?v=123">

You'll only need to change the version in the HTML after you've updated your css. Remember, your css file will always be named just main.css

Hope that helps!

Adam Konieska
  • 2,805
  • 3
  • 14
  • 27
1

Just to clarify this is called querystring caching. You add the querystring in the HTML only. When you change it most browsers will assume it's a new file and reload it regardless of how you set the cache for CSS files.

It's possible to do this via PHP without having to manually change it every time you change the css file.

<link rel="stylesheet" href="styles/main.css?t=<?php echo filemtime( 'styles/main.css'); ?>" type="text/css" media="screen" />

This appends the css files modification time to the HTML. Every time you save the CSS file this will update automatically.

Rick Calder
  • 18,310
  • 3
  • 24
  • 41
1

By this type of link, you passed to browser the HTTP request GET, with argument v and its value 123. Server is still looking for main.css. The 404 code means page not found.

If you want to server handles it use URL-rewriting. Or you can try encode the question mark by typing %3F.

Please note that file names containing question marks aren't allowed on windows and MSDOS systems.

Ann
  • 11
  • 2