SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,203
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)

    Setting the Content-Type on your server

    Most discussions about character encoding recommend that you set your pages' encoding on the server, with a meta declaration as a backup. When checking the response headers on many sites, I see this:

    Code:
    Content-Type: text/html; charset=UTF-8
    On my own server, I just see

    Code:
    Content-Type: text/html
    So I'm wondering how to ensure that the server is sending out pages with the charset also defined.

    I have a managed VPS with WHM/CPanel. I've looked everywhere for a way to set this, but haven't found much intelligible info. Am I barking up the wrong tree? This is not a desperate situation, just something I'd like to understand better. My pages work find as they are, but since I often read that the response headers are important I thought I'd follow up on this.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,203
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    Thanks logic_earth. I've seen that page before, but what I'm really after is info on how to access http.conf and how to edit it. I've sort of found out how to do this in WHM, via the Includes Editor. There's an option to add new stuff at the top or bottom of the file, and I'm not sure which is best. Aslo not sure if I can just add

    Content-Type: text/html; charset=utf-8

    and be done with it, or if there's some other code I need to add around it. You can take code from a .htaccess file and place it here. I'm not sure if the line above would be enough in a .htaccess file, or if something else is needed.

    Or perhaps I just need to add something like

    AddCharset UTF-8

    This is all a foreign language to me.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Use the .htaccess file
    Code:
    AddDefaultCharset utf-8
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,203
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    Cool, thanks. So just to clarify, I can add that line alone to the end of the http.conf file?

  6. #6
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by ralph.m View Post
    Cool, thanks. So just to clarify, I can add that line alone to the end of the http.conf file?
    You could put this in your http.conf file but it only gets loaded once; whereas putting it in a .htaccess file means that it gets run each time the .htaccess is called.
    ictus==""

  7. #7
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,203
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by ServerStorm View Post
    You could put this in your http.conf file but it only gets loaded once; whereas putting it in a .htaccess file means that it gets run each time the .htaccess is called.
    My aim really is to target all sites on the server in one hit. I'm not sure what you mean about loading once. Isn't a header sent out each time a page is requested? (I'm just stabbing in the dark here, I admit. But I'd love to understand this better. )

  8. #8
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by ralph.m View Post
    My aim really is to target all sites on the server in one hit. I'm not sure what you mean about loading once. Isn't a header sent out each time a page is requested? (I'm just stabbing in the dark here, I admit. But I'd love to understand this better. )
    In the case of targeting all sites on the server then using the
    Code:
    AddDefaultCharset utf-8
    in your http.conf is the best place to put it. This will direct all pages for all virtual hosts on the server to be of the UTF-8 default media type character set. Keep in mind that must save your html pages to be utf-8 encoded. It is also recommended that you set the language for your pages as automatic browser detection will use this if specified. To add the language do:
    HTML Code:
    <html lang="lang="en"">
    . If you use a database then the character set there also needs to be set to UTF-8 collation. The default collation of MySQL is 'latin1_swedish_ci'. If you forget to set the collation to UTF-8 before importing data you will need to go back and set it to UTF-8 and then have to re-import the data. This article may assist your understanding of MySQL's UTF-8 needs?

    One final point is that if you have an Apache directive such as AddDefaultCharset that does not need to run each time a page is requested then put it into your http.conf or a vhost.conf file that gets loaded once when Apache start up. If you want directives such as a rewrite directive that needs to run every time a resource is requested then you need to put it into the .htaccess file. Putting directives like AddDefaultCharset are considered wasteful (memory and processing) if put into .htaccess files.

    I hope this helps!

    Regards,
    Steve
    ictus==""

  9. #9
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,203
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    Thanks Steve. The article is over my head, I'm afraid. As a starting point, is there an easy way to tell what encoding (if any) the server may be using for the databases? It's easy to check the html page headers, but I'm not so sure about the databases.

    I'm not sure how important all of this is. Everything works fine on my sites, but I've always heard that it's the server's job to send out the encodings, so thought I should check up on this.

  10. #10
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by ralph.m View Post
    Thanks Steve. The article is over my head, I'm afraid. As a starting point, is there an easy way to tell what encoding (if any) the server may be using for the databases? It's easy to check the html page headers, but I'm not so sure about the databases.

    I'm not sure how important all of this is. Everything works fine on my sites, but I've always heard that it's the server's job to send out the encodings, so thought I should check up on this.
    Hi

    in PHPMyAdmin or MySQL Workbench or using MySQL on the command line you can run the following SQL:
    Code:
    SHOW VARIABLES LIKE 'character_set_database';
    and then run
    Code:
    SHOW VARIABLES LIKE 'character_set_client';
    these will show you the current character sets for your MySQL server and then the seconds shows the character set configuration in the client you are using. When you USE a database you can then run this SQL
    Code:
    SHOW CHARACTER SET;
    and
    Code:
    SHOW COLLATION LIKE 'utf8%';
    ; the first determines the character set of the database in use (you will know that it is use when you can run a SELECT statement in a given table and it runs correctly then you issue the aforementioned SQL. The second SQL tidbit will output all possible UTF-8 supported collations currently in your server.

    I don't use PHPMyAdmin, but I do use MySQL Workbench. In Workbench you can see the collation that is assigned to each table when you issue a Create or Alter command (via the GUI) This is a pretty easy way to see it; although doing the SQL commands I've shown you above will provide a complete picture in just a few lines of SQL.

    Steve
    ictus==""

  11. #11
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,203
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    Thanks Steve. In PHPMyAdmin, it says this:

    MySQL charset: UTF-8 Unicode (utf8)

    I don't know if this is telling me what the server is determining or whether it's just the setup for the current database (which is basically set up by the CMS I'm using, as DBs are outside my ken at this stage).

    I've (nervously) connected to the host account via the command line before, but am not sure how to get to the point you described above. Presumably there are commands first to connect with MySQL ...?

  12. #12
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by ralph.m View Post
    Thanks Steve. In PHPMyAdmin, it says this:

    MySQL charset: UTF-8 Unicode (utf8)

    I don't know if this is telling me what the server is determining or whether it's just the setup for the current database (which is basically set up by the CMS I'm using, as DBs are outside my ken at this stage).

    I've (nervously) connected to the host account via the command line before, but am not sure how to get to the point you described above. Presumably there are commands first to connect with MySQL ...?
    Hi, this is likely telling you what the current database is set to.

    To connect over the command line you typically do:
    Code:
    mysql --user=ralph.m --password=@!_h@teC0mMan6L1n3 db_name
    USE db_name;
    SHOW CHARACTER SET;
    You can also use the command line like:
    Code:
    mysql --user=user_name --password=your_password db_name
    USE db_name;
    SELECT first_name, last_name FROM people;
    When you hit return it runs the command of the line where your cursor is.

    You are on the right track
    ictus==""

  13. #13
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,203
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    Thanks Steve. Makes sense.

    Quote Originally Posted by ServerStorm View Post
    This will direct all pages for all virtual hosts on the server to be of the UTF-8 default media type character set. Keep in mind that must save your html pages to be utf-8 encoded.
    Just to go back a bit, what did you mean by that second sentence? I always include the meta element, so is that what you meant? I wasn't sure how important that is if the server has specified the encoding. I thought it was really for when the page is offline etc.

  14. #14
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by ralph.m View Post
    Thanks Steve. Makes sense.



    Just to go back a bit, what did you mean by that second sentence? I always include the meta element, so is that what you meant? I wasn't sure how important that is if the server has specified the encoding. I thought it was really for when the page is offline etc.
    Hi Ralph,

    Certain editors allow you to save documents in a particular encoding such as UTF-8. Note pad on Windows will not do this but if you search for text encoding Mac then you should find an application that you can use to save your .html/.php files as UTF-8 encoded. if you don't do this it won't matter if you've set UTF-8 in the header the document will not be in UTF-8. The meta element simply tells the brower what encoding to switch to rather than auto detecting it; however just because you set this does not mean that the html document is encoded as UTF-8 that is why you need to save your html/php files with UTF-i8 encoding. By encoding it you will have the header, Apache, the html file, the database and the text encoding of the html file to all be aligned in UTF-8 and will be serving it up
    ictus==""

  15. #15
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,203
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    Ah, I see what you were referring to. Yes, I do that by default. Thanks Steve.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •