Forum Moderators: phranque

Message Too Old, No Replies

htaccess basics

how to structure rules

         

Reid

8:55 am on Mar 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok I'm about to implement my first htaccess file.
There are 2 things I want to do
1. protect my image files from leeching.
2. specify a couple of redirects for files I deleted many moons ago but still get the odd 404 over.

here is the leech protection script I got from my host provider.


RewriteEngine On
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!yourdomainname [NC]
RewriteRule .*\.(gif¦jpeg¦jpg¦png)$ - [F]

how do I add my redirct to this file like this?


RewriteEngine On
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!yourdomainname [NC]
RewriteRule .*\.(gif¦jpeg¦jpg¦png)$ - [F]
RewriteRule ^old.html$ /new.html [R]

or like this?


RewriteEngine On
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!yourdomainname [NC]
RewriteRule .*\.(gif¦jpeg¦jpg¦png)$ - [F]
RewriteEngine On
RewriteRule ^old.html$ /new.html [R]

or like this?


RewriteEngine On
RewriteRule ^old.html$ /new.html [R]
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!yourdomainname [NC]
RewriteRule .*\.(gif¦jpeg¦jpg¦png)$ - [F]

also I suppose I just load this htaccess file into the root directory right?

jdMorgan

4:22 pm on Mar 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Reid,

Since the rules are mutually exclusive based on filetype, their order doesn't matter.
Only one RewriteEngine on directive is needed.
The code can be cleaned up a bit, and contains several errors.


RewriteEngine On
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !yourdomain\.com [NC]
RewriteRule \.(gif¦jpeg¦jpg¦png)$ - [F]
#
RewriteRule ^old\.html$ http://yourdomain.com/new.html [R=301,L]

Note that literal periods inside regular-expressions patterns must be escaped by preceding them with "\" as shown, (and only where shown).

Use a 301 redirect and a canonical URL -- rather than the default 302 and a local path -- if the pages have been renamed permanently. Failure to do so may cause unpleasant results in your search engine listings.

Use the [L] flag to stop mod_rewrite processing and redirect immediately when a rule matches. Using this flag can significantly speed up processing of long lists of rules if they are ordered in most-likely-to-match-first order. Use the [L] flag unless you actually want to process the output of a given rule through all subsequent rules.

You may also need to add


Options +FollowSymLinks
at the top of this code in order to get it to work on your server. Whether you need to do this depends on how your host has configured the server.

.htaccess files can go into any Web-accessible directory on your site. In this case, put the .htaccess file into the same directory as your "home page."

Posting on this forum modifies some charactrers. Change the broken pipe "¦" characters back to solid pipes before trying to use code posted here.

A bit of friendly advice: Test your simplest rule first. Then add a few more. Don't try uploading a huge file of untested rules onto a server you've never tested mod_rewrite on before. This way lies madness. If you do have trouble, the first place to look is in your server error log file -- It will often tell you specifically what is wrong.

When testing access-control code like this, flush your browser cache (Temporary Internet Files) before testing any change to the code. Otherwise, the cached copies of requested resources will cause invalid test results (Your rules can only affect resources fetched from your server. If the resources are cached locally, the browser will use the cached copies, and they won't be fetched from the server, and your rules will have no effect).

Our forum charter [webmasterworld.com] includes links to several mod_rewrite-related resources.

Jim

Reid

2:20 am on Mar 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Jim
It's a pretty steep learning curve at first but I'm getting it more and more.

Funny that 'code full of errors' was supplied by the help section of my hosting service.
Pretty typical.

I suppose that number symbol seperates directives.

jdMorgan

5:38 am on Mar 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The "#" symbol is used to denote a comment, or a blank line inserted for formatting purposes.

Jim

Reid

12:16 am on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is this proper syntax?
first I want to deny access to a perticular domain
second i want to 301 a page I deleted a while back that I still get requests for -

Order Deny,Allow
Deny from www.loser.com
Allow from all
#
RewriteEngine On
RewriteRule ^old\.html$ http://mydomain.com/new.html [R=301,L]

The image leetch protection I have working in my other directory because there are some images in my root that I want accessed.
Ive been studying I'm just having trouble findind info about adding different types of directives to the same file. is this how it's done?

edited because of error I noticed.

[edited by: Reid at 12:22 am (utc) on Mar. 11, 2005]

zeus

12:21 am on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I got a question here has anyone here a full example of a .htaccess that let the importent SE images search be dispayed, but ban the rest of the hotlinking.

I have a apache dedicated server, Linux

jdMorgan

2:28 am on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to use Allow,Deny if you want to deny access to loser.com. The Order directive sets the priority of the Allow and Deny directives, and has nothing to do with the order in which you list them in your file.

Order [b]Allow,Deny[/b]
Deny from www.loser.com
Allow from all
#
RewriteEngine On
RewriteRule ^old\.html$ http://mydomain.com/new.html [R=301,L]

Everything else is OK.

Jim

Reid

5:03 am on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've done it exactly as it appears there, even added deny relating to the IP address, but the link still works. even cleared my history.

jdMorgan

7:01 am on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Clear your browser cache (Temporary Internet Files).

One more thing. Your code will stop the server located at loser.com from accessing your server. Is that what you want to do, or are your trying to block referrals from loser.com?

Jim

Reid

8:04 am on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was trying to block referrals from loser.com but I have since found out that is not the way.

One thing I am scrattching my head over Jim is that I found out it is a good idea to put a 301 redirect to make your URL cannonical in order to prevent google from spidering yur site twice and penalizing you for duplicate content.

I've tried every possible combination to do this but when I use an online server header checker I always get the same results.
When I test mysite.com it responds 302 found
When I test www(dot)mysite.com it reponds 200 ok

This is what I am using :

 RewriteEngine on 
RewriteCond %{HTTP_HOST} ^mysite\.com
RewriteRule ^(.*)$ ht(ignore)tp://www.mysite.com/$1 [R=permanent,L]

I tried adding

Options +FollowSymLinks 
above it but that causes 500 internal server error.

I'm wondering if it is because my virtual host is already doing a 302 upstream or what?
If they are this could be a problem?

Reid

8:10 am on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



They must be doing it upstream because even without this in my htaccess it still redirects 302.

jdMorgan

8:48 pm on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you got the 500-Server Error, what did your server error log have to say about it? Critical info in there...

Jim

Reid

11:06 pm on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't have access to the server error logs.

My webhost has it set up like this

my temporary url: servername.myhost.~mysite.com

my url: normal cannonical url

Reid

9:21 pm on Mar 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wrote to my webhost support about this detailing the duplicate-content issue with a link to the news article about big dot com banning itself with a 302 redirect from big.com to w*w.big.com and this is what they said.

Summary of host's e-mail response:

Your domain is hosted on a shared-hosting server. Some mod-rewrite rules take precedence over your own. Long before the rules in your .htaccess file ever gets processed, the server-wide URL rewrites have already taken effect.

Unfortunately, these server-wide settings force a 302 Temporarily Moved response header, instead of the desired 301. While it may seem like a minor change on our part to change the default method of redirection, this is not something that we can do easily because of the server management software that we use and the scripts that depend on the 302 header. I will notify the administrators about this issue, though.

I have a friend who uses a different 'shared hosting server' and his does not use a 302 redirect so they got 2 months (before renewal) to fix it - or at least make me feel better. I haven't had a problem so far - I think they are blocking robots on the other side but what if someone links to me without the www? that would be a 302 to me.

[edited by: jdMorgan at 5:15 pm (utc) on Mar. 27, 2005]
[edit reason] Summarized e-mail response. See TOS. [/edit]

jdMorgan

5:09 pm on Mar 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Their "requirement" to force a 302 on you when it endangers your ranking is unacceptable. You might indeed want to consider changing hosts.

Jim