Forum Moderators: phranque

Message Too Old, No Replies

redirect IP based all pages to domain

IP redirection works but for pages it does not work

         

phparion

8:22 am on Jul 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi,

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.

jdMorgan

2:54 pm on Jul 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is this code in .htaccess or in a server config file?

It looks "almost correct" and actually is "correct enough" to work, if not efficiently.

Did you completely flush (delete) your browser cache before testing?

Jim

phparion

2:56 pm on Jul 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



this code is in .htaccess. yes I have cleared the cache. the pages like 1.2.3.4/pagename/ do not redirect to the domain respective pages.

jdMorgan

4:17 pm on Jul 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Clean up the code and see if this helps:

RewriteCond %{HTTP_HOST} ^1\.2\.3\.4\.?(:[0-9]+)?$
RewriteRule ^(.*)$ http://www.example.co[b]m/$[/b]1 [R=301,L]

Note that "/pagename/" is incorrect URL-usage. If it's a page, it should be "/pagename" - no trailing slash. If it's intended to be a directory-index, it should be "/directoryname/". That won't affect your problem, though -- it's more about Web standards.

Jim

phparion

8:44 am on Jul 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi 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!

jdMorgan

4:10 pm on Jul 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> ... one of the URLs had a blog installation in it and blog's htaccess was overtaking the effect of main .htaccess file.

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