Forum Moderators: phranque
google has indexed my site's few pages with IP address. pages are like
1.2.3.4/pagename/
where "pagename" is a variable.
I want to redirect all IP based URLs to their respective Domain based URLs.
I used the following rewrite rule,
RewriteCond %{HTTP_HOST} ^1\.2\.3\.4(?::.*)?
RewriteRule (.*) http://www.example.com$1 [R=301,L]
but it redirects only [1.2.3.4...] and not pages like [1.2.3.4...]
Any wise words are much appreciated.
RewriteCond %{HTTP_HOST} ^1\.2\.3\.4\.?(:[0-9]+)?$
RewriteRule ^(.*)$ http://www.example.co[b]m/$[/b]1 [R=301,L]
Jim
Thank you for your replies. The rewrite rule was fine, the only problem was that one of the URLs had a blog installation in it and blog's htaccess was overtaking the effect of main .htaccess file.
So I wrote the rewrite rule in blog's htaccess and everything is fine now.
a quick question about the rewrite rule you suggested last,
^1\.2\.3\.4\.?(:[0-9]+)?
if i explain it, it is "any url which has the IP, optionally followed by any character or a semicolon (for port #) followed one or more digits"
can we write the same thing as
^1\.2\.3\.4(.*)?
thank you!
See RewriteOptions Inherit
> ... it is "any url which has the IP, optionally followed by any character or a semicolon (for port #) followed one or more digits"
No, it is "Match any hostname which has the IP address, optionally followed by a literal period and then optionally, a colon followed one or more digits."
... or functionally, "Recognize only the valid IP address, possibly in FQDN format and/or with an appended port number." That is, accept it only if it's a valid IP-address-hostname request.
You could also use
^1\.2\.3\.4[.:]?
leaving the pattern without an end-anchor, but specifying that if any character follows "\.4", it must be either a period or a colon and not, for example, another IP-address digit. Note that escaping rules differ within alternate-character groups, and that within a group, the period does not need to be escaped.
Avoid the use of the ".*" pattern whenever possible -- It is a bad pattern to get into the habit of using, for many reasons. Note that in all of the examples in the Apache URL Rewriting Guide, it is hardly ever used because a more-specific and a more-efficient pattern can almost always be used.
Also, "(.*)?" is redundant at the regex-meaning level, and either ".*" or just ".?" would do the same thing unless the pattern was end-anchored.
Jim