Forum Moderators: phranque

Message Too Old, No Replies

help with htaccess

need some opinions

         

mihomes

8:55 pm on May 10, 2011 (gmt 0)

10+ Year Member



I am moving away from a script which would set correct headers and caching and need to do so manually through htaccess now...

This is what I have currently :

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule (.*) [mysite.com...] [R=301,L]
# BEGIN expires rules
<IfModule mod_expires.c>
ExpiresActive on
<FilesMatch "\.(jpe?g|gif|png|ico|pdf|swf|flv)$">
ExpiresDefault "access plus 20 days"
</FilesMatch>
<FilesMatch "\.(js|css|txt)$">
ExpiresDefault "access plus 10 days"
AddDefaultCharset UTF-8
</FilesMatch>
<FilesMatch "\.(html?|php)$">
ExpiresDefault "access plus 2 days"
AddDefaultCharset UTF-8
</FilesMatch>
ExpiresDefault "access plus 1 hours"
</IfModule>
# END expires rules
# BEGIN compress files
<IfModule mod_deflate.c>
<FilesMatch "\.(html?|php|js|css|rss|xml)$">
SetOutputFilter DEFLATE
Header append Vary Accept-Encoding
</FilesMatch>
</IfModule>
# END compress files

The goal here was to make use of compression, caching, etc... is is a very basic and small site with htm/php files, js, css, and images... pages will not change much except a modification here and there.

Anyone see anything wrong, missing, or recommend? I see a lot of people use a cache-control public, must-revalidate, private, etc... are those recommended?

AddDefaultCharset UTF-8 was added because my pages were not specifying a charset eventhough I use meta tags in the page to do so.

Header append Vary Accept-Encoding was added as was recommended by google pagespeed.

Appreciate any help. I want to get something correct in stone on one site and then go ahead and implement on my others.

g1smd

9:21 pm on May 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The non-www to www redirect doesn't redirect all non-canonical URLs.

Change the
RewriteCond
pattern to
!^(www\.example\.com)?$
to fix that.

mihomes

10:06 pm on May 10, 2011 (gmt 0)

10+ Year Member



hmm... I never had a problem with that. I just tested it as well with

MySite.com, MYSITE.com and they redirect to www.mysite.com.

When I try something like Mysite.Com/Index/ it will redirect to www.mysite.com/Index/

Perhaps this is a settings I have by default on my server.

mihomes

10:48 pm on May 10, 2011 (gmt 0)

10+ Year Member



Here is something I noticed... in the code above I have set the default charset to utf-8 for htm, html, php, css, js, txt...

When using liveheaders in ff it works properly for htm/html/php, but for js and css it shows doesnt show utf-8 rather something like :

Content-Type: text/css

Now, if I change my htaccess to :

AddType 'text/css; charset=UTF-8' css

then the response in liveheaders is :

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

Does that mean that using the AddDefaultCharset UTF-8 for js and css is not working or it is and just not showing the utf-8?

mihomes

11:19 pm on May 10, 2011 (gmt 0)

10+ Year Member



After doing a little research it appears that AddDefaultCharset is only for text/html... therefore the reason it was not being applied to css and js.

Current file is as of now listed below. Comments and recommendations please... I am really interested to hear thoughts regarding use of public, private, revalidate, etc being added into this... pros and cons...

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule (.*) [mysite.com...] [R=301,L]
# BEGIN defaults
DefaultLanguage en-us
AddDefaultCharset utf-8
AddType 'text/css; charset=utf-8' css
AddType 'application/javascript; charset=utf-8' js
# END defaults
# BEGIN expires rules
<IfModule mod_expires.c>
ExpiresActive on
<FilesMatch "\.(jpe?g|gif|png|ico|pdf|swf|flv)$">
ExpiresDefault "access plus 20 days"
</FilesMatch>
<FilesMatch "\.(js|css|txt)$">
ExpiresDefault "access plus 10 days"
</FilesMatch>
<FilesMatch "\.(html?|php)$">
ExpiresDefault "access plus 2 days"
</FilesMatch>
ExpiresDefault "access plus 1 hours"
</IfModule>
# END expires rules
# BEGIN compress files
<IfModule mod_deflate.c>
<FilesMatch "\.(html?|php|js|css|rss|xml)$">
SetOutputFilter DEFLATE
Header append Vary Accept-Encoding
</FilesMatch>
</IfModule>
# END compress files

g1smd

11:30 pm on May 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



www.example.com:80/
isn't redirected by your code and nor is
www.example.com./
either.

lucy24

11:35 pm on May 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Does that mean that using the AddDefaultCharset UTF-8 for js and css is not working or it is and just not showing the utf-8?

Different answer: Does your css and/or js contain literal strings that will be sent back to the HTML page, and does that text contain non-ASCII characters? Doesn't have to be anything fancy; a single curly apostrophe will do. If not, the "charset" ("file encoding" is a better term, but we're stuck with charset) should not make a speck of difference, because it's all ASCII-7 anyway.

mihomes

3:02 am on May 11, 2011 (gmt 0)

10+ Year Member



www.example.com:80/ isn't redirected by your code and nor is www.example.com./ either.

Just tried both of those with the site in question and both redirect properly to www.example.com with the code I originally listed...

mihomes

3:33 am on May 11, 2011 (gmt 0)

10+ Year Member



I take that back g1smd... the ./ wasn't redirecting properly as you said... Just made the change as you recommended and it works perfectly.

I have an older domain which I redirect to the one in question... I need to do both www and non-www and take care of the non-canocalization as you mentioned. Would this be correct? It seems to work in all tests I have made, but is there a way to shorten it?

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.oldsite\.com)?$
RewriteRule (.*) [newsite.com...] [R=301,L]
RewriteCond %{HTTP_HOST} !^(oldsite\.com)?$
RewriteRule (.*) [newsite.com...] [R=301,L]

g1smd

7:03 am on May 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The code in the last post is very sketchy.

First rule. If domain is not "exactly" www.exampleoldsite.com then redirect to www.examplenewsite.com.

Second Rule. If domain is not "exactly" exampleoldsite.com then redirect to www.examplenewsite.com.

Simplified:
RewriteCond %{HTTP_HOST} ^(www\.)?exampleoldsite\.com
RewriteRule (.*) http://www.examplenewsite.com/$1 [R=301,L]


New rule: If domain begins with exampleoldsite.com or with www.exampleoldsite.com then redirect. With no end anchor it also redirects if port or period present. This rule goes on the old site, assuming it is separately hosted.

If you could instead point the old domain name at the new hosting, all you would need is this:

RewriteCond %{HTTP_HOST} !^(www\.examplenewsite\.com)?$
RewriteRule (.*) http://www.examplenewsite.com/$1 [R=301,L]


With start and end anchoring, and the ! for not, it now says: if domain is not "exactly" www.examplenewsite.com then redirect to www.examplenewsite.com.

The question mark is there to detect blank hostname value in the request if the client uses HTTP/1.0. Without that, HTTP/1.0 requests would hit an infinite redirect loop.

Use www.example.com to stop the forum auto-linking the URL.

mihomes

4:22 pm on May 11, 2011 (gmt 0)

10+ Year Member



Very much appreciated - that makes sense... I modified your first rule to :

RewriteCond %{HTTP_HOST} ^(www\.)?exampleoldsite\.com
RewriteRule (.*) [examplenewsite.com...] [R=301,L]

As on the old domain I simply want everything going to the new domains index.

mihomes

4:42 pm on May 11, 2011 (gmt 0)

10+ Year Member



Here is the current updated code. I would still appreciate any comments, recommendations on the caching aspects of everything, particularly, the use of public, private, revalidate, etc. My goal with this test-case is to satisfy google pagespeed, yslow, etc and remove the use of a wrapping php code which performed all of the correct headers/caching/etc for me int the past.


RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.mysite\.com)?$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
# BEGIN defaults
DefaultLanguage en-us
AddDefaultCharset utf-8
AddType 'text/css; charset=utf-8' css
AddType 'application/javascript; charset=utf-8' js
# END defaults
# BEGIN expires rules
<IfModule mod_expires.c>
ExpiresActive on
<FilesMatch "\.(jpe?g|gif|png|ico|pdf|swf|flv)$">
ExpiresDefault "access plus 20 days"
</FilesMatch>
<FilesMatch "\.(js|css|txt)$">
ExpiresDefault "access plus 10 days"
</FilesMatch>
<FilesMatch "\.(html?|php)$">
ExpiresDefault "access plus 2 days"
</FilesMatch>
ExpiresDefault "access plus 1 hours"
</IfModule>
# END expires rules
# BEGIN compress files
<IfModule mod_deflate.c>
<FilesMatch "\.(html?|php|js|css|rss|xml)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
# END compress files