...

View Full Version : .htaccess and order of code?



student101
04-15-2009, 11:39 AM
How do I know that I placed the correct code in the right places in the .htaccess file?

Anyone able to assist me in checking my .htaccess file and seeing if it's correct?

Cheers

schleppel
04-15-2009, 10:30 PM
How do I know that I placed the correct code in the right places in the .htaccess file?
I believe the processing is done one module at a time. So for directives from different modules, the order usually does not matter. For example

ErrorDocument 404 /abc.html
Redirect 302 /def http://www.example.com/ghi
and

Redirect 302 /def http://www.example.com/ghi
ErrorDocument 404 /abc.html
do the same thing.

for rules within some modules it might make a difference

RedirectMatch 302 ^/abc http://www.example.com/ghi
RedirectMatch 302 ^/a http://www.example.com/jkl
will redirect /abc to /ghi and

RedirectMatch 302 ^/a http://www.example.com/jkl
RedirectMatch 302 ^/abc http://www.example.com/ghi
will redirect /abc to /jkl.

But it might not make a difference

ErrorDocument 404 /404.html
ErrorDocument 401 /401.html


Anyone able to assist me in checking my .htaccess file and seeing if it's correct?
Post it.

student101
04-15-2009, 10:39 PM
Attached is a copy of my .htaccess file that I currently use (domain shown as example.com)

schleppel
04-15-2009, 11:29 PM
There's nothing major wrong with it. There's nothing wrong (as far as i can see) with the order.

You only need one

RewriteEngine On

When processing should stop after a RewriteRule matches, the L flag should be used. For example where you are using F(orbidden) and G(one).

student101
04-15-2009, 11:36 PM
Cool,
I see you changed the IP.* to IP\., wasn't too sure of that format...

Let me test your version, for some reason only Opera obeyed the blocking of my own IP address for testing.

I must review your version of shorthand to my loooooooooong version and learn some more.

Thank you.

schleppel
04-16-2009, 12:12 AM
Let me test your version, for some reason only Opera obeyed the blocking of my own IP address for testing.
HTTP_USER_AGENT is not the user's IP address (REMOTE_ADDR is). You are probably blocking because of a version number, for example

Mozilla/5.0 (Windows NT 5.1; U; en-GB; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.61
could be blocked by

RewriteCond %{HTTP_USER_AGENT} 200.*


I must review your version of shorthand to my loooooooooong version and learn some more.
I combined


RewriteCond %{HTTP_REFERER} www.macedonia-tour-travel\.com [NC,OR]
# ....etc...
RewriteCond %{HTTP_REFERER} deepakbansal\.com
into

RewriteCond %{HTTP_REFERER} (www\.macedonia-tour-travel\.com|...etc...|deepakbansal\.com) [NC]
with regular expression alternation (http://www.regular-expressions.info/alternation.html).

I then combined

RewriteCond %{HTTP_REFERER} long regular expression [NC]
RewriteRule .* - [G]
and

RewriteCond %{HTTP_USER_AGENT} long regular expression [NC]
RewriteRule .* - [G,L]
with the OR RewriteCond flag

RewriteCond %{HTTP_REFERER} long regular expression [NC,OR]
RewriteCond %{HTTP_USER_AGENT} long regular expression [NC]
RewriteRule .* - [G,L]

student101
04-16-2009, 12:31 AM
Wow you know your .htaccess, pretty neat!
I wanted to send bad-bots/spammers to my own 404 page but I think I need to set the AllowOverride or ask the host to change one of the Apache files to help.
I haven't figured out what IfModule mod_rewrite.c means yet.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^$ [OR]
RewriteCond %{HTTP_USER_AGENT} ^.*(<|>|'|%0A|%0D|%27|%3C|%3E|%00).* [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*(HTTrack|clshttp|archiver|loader|email|nikto|miner|python).* [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*(winhttp|libwww\-perl|curl|wget|harvest|scan|grab|extract).* [NC]
RewriteRule ^(.*)$ http://domain.tld/errorpage.php?error=404 [G,L]
</IfModule>

schleppel
04-16-2009, 11:57 PM
I wanted to send bad-bots/spammers to my own 404 page but I think I need to set the AllowOverride or ask the host to change one of the Apache files to help.
Why (the AllowOverride part)? What problem are you having?


I haven't figured out what IfModule mod_rewrite.c means yet.
Directives inside an <IfModule> (http://httpd.apache.org/docs/2.2/mod/core.html#ifmodule) will only be processed if the module (in this case mod_rewrite) is loaded (or if it's !module if the module is not loaded).

student101
04-17-2009, 12:12 AM
I can't seem to get the error404 page to show.
I only see the hosts error pages

I attempted the code I posted above but it doesn't work.

schleppel
04-17-2009, 12:56 AM
Are you using a free host? They might be forcing their pages so they can put ads on them.

student101
04-17-2009, 08:04 AM
No, it's paid hosting, no ads just this;

Gone
The requested resource
/
is no longer available on this server and there is no forwarding address. Please remove all references to this resource.
Apache/2.2.3 (CentOS) Server at www.example.com (http://www.example.com) Port 80

schleppel
04-17-2009, 06:57 PM
Oh i see. Try adding an ErrorDocument (http://httpd.apache.org/docs/2.2/mod/core.html#errordocument)

ErrorDocument 410 /errorpage.php?error=404

student101
04-17-2009, 08:53 PM
Ok it's the correct code for the "gone" page.
Cool, thanks!
No, for some reason that doesn't work.

schleppel
04-19-2009, 04:50 PM
It works fine for me, what is your full .htaccess file?

You could use

RewriteRule ^(.*)$ /errorpage.php?error=404 [L]
and send the Gone header with PHP's header().

student101
04-19-2009, 05:35 PM
It's the exact same one you posted back.
If I use: [F,L] I Get Apache 2 Test Page
If I use: [G,L] or [L] I Get this:


Gone
The requested resource
/
is no longer available on this server and there is no forwarding address. Please remove all references to this resource.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

schleppel
04-19-2009, 06:38 PM
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
I believe the request to /errorpage.php?error=404 matches the RewriteRule and causes a loop.

Try adding

RewriteCond %{ENV:REDIRECT_STATUS} ^$
to all [G,L] rules.

student101
04-19-2009, 07:03 PM
Yes that's it!, sometimes rewrites cause infinite loops, forgot to stop it with one of the REDIRECT_STATUS.

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [G,L]

Thank you!
It works, glad you saw that one.

Cheers

Not using actual status code only the ^$



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum