Forum Moderators: phranque
Welcome to WebmasterWorld!
You need a RewriteRule pattern to specify what requests you want to rewrite. in most cases, this should be only files of the same type as those you are rewiting to. In addition, you have to take steps to avoid recursion, by preventing the new URL itself from being rewritten:
RewriteEngine on
RewriteCond %{REMOTE_HOST} ^the\.bad\.domain\.net$
RewriteCond %{REQUEST_URI} !^/pagetoshow\.html$
RewriteRule \.html$ /pagetoshow\.html [L]
See the resources cited in our forum charter.
Jim
I assume i am missing something here
(Haven't been able to get it to work yet.)
I thought i should test this using my isps' information that comes back as below when testing it
ip33-333-33-33.bad.domain.net.
I got the above format back when using this to test:
<?php
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
echo $hostname;
?>
Of course that used 'REMOTE_ADDR' to get at the
ip33-333-33-33.bad.domain.net
(not REMOTE_HOST as used below)
When i tested nothing apparent happened unless I had
something like:
RewriteCond %{REMOTE_HOST} ^*the\.bad\.domain\.net$
(I was trying to match for all the numbers & hypens in
the ip address with the *)
This generated an internal server error again.
I guess my questions are with:
RewriteEngine on
RewriteCond %{REMOTE_HOST} ^the\.bad\.domain\.net$
# Is REMOTE_HOST or REMOTE_ADDR what i need above or #maybe my regex is #incorrect based on the longer
#"ip33-333-33-33.bad.domain.net"
RewriteCond %{REQUEST_URI}!^/pagetoshow\.html$
# I think I understand the immediate line above to mean #when trying to request i.e. www.domain.com #/pageurltorewrite.html use the RewriteRule below
#(I have not been able to find out what the regex #symbol (! exclamation) means though )
RewriteRule \.html$ /pagetoshowinstead\.html [L]
#I think the above means whenever anypage.html is requested that pagetoshowinstead.html is supplied in it's place.
I hope this is not too long winded or too confusing of an explanation. I am really trying to sort this all out.
Thanks again & Best,
Jeff
1) The most important line in my first reply was this:
> See the resources cited in our forum charter.
2) See #1 above. :)
Your regex is invalid, because "*" doesn't mean what you intend. In regular expressions, "*" means "any number (including zero) of the previous character." You want ".*" which means "any single character" followed by "*" which means "any number (including zero) of the previous character." Taken together they mean, "any number (including zero) of any character."
However, since that sequence immediately followed the "^" start anchor, this means you don't need a start anchor, and can simply use an unachored pattern. This is all "regex-speak" -- see #1 and #2 above if it's not clear.
The RewriteCond I added prevents an infinite loop after the first rewrite. I've added comments to the code below:
# Enable rewrite engine
RewriteEngine on
# IF request is NOT for "/pagetoshow.html"
RewriteCond %{REQUEST_URI} !^/pagetoshow\.html$
# AND IF request is from bad domain
RewriteCond %{REMOTE_HOST} [b]t[/b]he\.bad\.domain\.net$
# THEN rewrite all requests for html files to "/pagetoshow.html" and quit
RewriteRule \.html$ /pagetoshow\.html [L]
If you can get the actual IP address of the unwelcome visitor from your raw access logs, use that and use {REMOTE_ADDR} to control access. This is much more efficient that using the hostname and {REMOTE_HOST}.
Understand that if you use {REMOTE_HOST}, then for each and every .html page requested from your site, your server will have to generate and send a reverse-DNS lookup request to the DNS network. It will send the IP address of the requesting client (REMOTE_ADDR) to the DNS system, and ask for the associated hostname (REMOTE_HOST). It will then have to wait for the reverse-DNS response; The original .html page request cannot be served until this reverse-DNS request is fulfilled. This adds unnecessary delay, slowing down your server, and also makes your server dependent upon the reverse-DNS provider; if it fails, the requests to your server will fail as well. For this reason, it is far better to control access using {REMOTE_ADDR} which is directly available in the HTTP request received by your server.
You php example clearly illustrates the reverse-dns process; "gethostbyaddr" is the reverse-DNS request for the remote hostname, using the REMOTE_ADDR as the argument.
Using REMOTE_ADDR, the code becomes:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/pagetoshow\.html$
RewriteCond %{REMOTE_ADDR} ^192\.168\.10\.17$
RewriteRule \.html$ /pagetoshow\.html [L]
If, after making the changes shown above, you still get a 500-Server Error, then it's likely you'll need to add
Options +FollowSymLinks
Please do not post "real" domain names or IP addresses here. Under our terms of service (see below) I must edit them out. This can lead to confusion when discussing access control, so it is better if you pick the "example" domain names, so that replies can be consistent with your example. Exceptions are made for non-routable IP addresses and for URLs leading to well-recognized and authoritative or primarily non-commercial sources, such as CNN or apache.org.
Thanks,
Jim
Thank you for your time & explanation of my questions about the syntax. I went to the resources & then tried to write the questions as I thought I understood them somewhat. I know you couldn't possibly have time to write code for everyone.
I went so crazy trying to look up the (! incorrectly as a RegEx character) that I it made it difficult for me to understand that is the statement which would prevent the infinite loop. I should have seen it as the negate.
Thanks again. This really made my day getting this in my brain more. (By the way it is working :) )
Sincerely & Best,
Jeff