Forum Moderators: phranque

Message Too Old, No Replies

Need tip about .htaccess

htaccess question

         

gomez123

2:06 pm on Oct 27, 2009 (gmt 0)

10+ Year Member



Hello guys, I use google adsense on my website. Somehow google adsense crawler didn't extract the content correct for my homepage. When I open http://www.example.com/ it displays public ads ( which are not profitable ) and in case I open http://example.com/ or http://www.example.com/index.php the ads are relevant to my content. That is normal as google cannot determine the content of http://www.example.com/ properly, for some reasons.
In that case I'm forced to trick it, so I was wondering how to set up my page, so that when my visitors open http://www.example.com/ to show the index.php. I know it's something very easy, but I can't figure it out.

Posting my .htaccess file:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?%{QUERY_STRING}&resource=$1 [L]

Any help would be appreciated.

jdMorgan

3:23 pm on Oct 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not clear what your question is. If your DirectoryIndex directive is properly configured, then a request for the URL www.example.com/ should automatically open the file /index.php. Further, if you have the recommended URL and domain canonicalization measures in place, it should be impossible for "/" and "/index.php" to be 'different', and direct client requests for "index.php" in any subdirectory should be externally redirected to "/" in that same subdirectory.

You can associate "/" with "/index.php" using DirectoryIndex, canonicalize /index.php to "/" and force the canonical "www" subdomain by adding two redirect rules, and make your existing rewrite rule more efficient by using the [QSA] flag to append your additional query parameters to any requested query instead of "doing it manually":


DirectoryIndex index.php
#
RewriteEngine on
#
# Externally redirect direct client "/index.php" requests to canonical "/" URL in same subdirectory
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]*/)*index\.php(#[^?]*)?(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect non-canonical hostname requests to canonical domain
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# Internally rewrite all requests which do not resolve to existing files to /index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?resource=$1 [QSA,L]

This code may need adjustment so that the "www.example.com" matches your real domain; Be sure that all references to it are entirely consistent except for the required character-escaping as shown. Be sure that you understand this code and everything that it does before you try to use it -- See the references cited in our Forum Charter for more information.

Jim

jdMorgan

3:35 pm on Oct 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I also suggest adding some exclusions to the now-final rule above. You should exclude 'obvious' URLs such as references to images and 'reserved' files from being tested for 'file exists', since these should probably never be rewritten to your script. Add a RewriteCond such as

RewriteCond $1 !(\.gif¦\.jpe?g¦\.png¦\.ico¦\.css¦\.js¦sitemap\.xml¦^robots\.txt¦^labels\.rdf¦^w3c/p3p\.xml)$

as the first RewriteCond for your last rule to prevent your server from having to go check the filesystem for each and every HTTP client request. This can make a *huge* improvement in your server performance as your site gets popular/busy.

Replace the broken pipe "¦" characters with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

gomez123

6:13 pm on Oct 27, 2009 (gmt 0)

10+ Year Member



Jim, you are the one and only ! It works like a charm !
I'm very new to htaccess, but not that lame to say that you are the man :) I just copied the lines that you posted, now I will review them and try to understand the logic. Thank you for you second post as well !
If I make something from that ads will let you know and share :) Thanks again ! So professional...

wildbest

6:36 pm on Oct 27, 2009 (gmt 0)

10+ Year Member



Jim,

What is the difference between:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$

and:

RewriteCond %{HTTP_HOST} !=www.example.com

jdMorgan

7:40 pm on Oct 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The former excludes either a blank or exact-match hostname from being redirected, while the latter only excludes an exact match. If you are hosted on an IP-based server --one that has a unique IP address-- then the second method can result in an 'infinite' redirection loop if a request is received from a true HTTP/1.0 client. This is because an HTTP/1.0 by definition cannot send a "Host:" header, and so would be repeatedly redirected. Although true HTTP/1.0 clients are obsolete, it's still a risk to be avoided.

I use the term "true HTTP/1.0 clients" to distinguish them from clients that actually do support some or all ot the HTTP/1.1 protocol extensions and that do send a "Host:" request header, but that continue to 'publish' as HTTP/1.0 clients for compatibility reasons. For example, some versions of Googlebot do this.

It's OK to use "=www.example.com" for an exact positive match, but using it for a negative match would require a second RewriteCond to prevent the HTTP/1.0 looping problem.

Jim