Forum Moderators: phranque

Message Too Old, No Replies

Redirect IP address to URL

Redirect all pages in folder with IP address to URL

         

robinantill

7:17 pm on Aug 26, 2012 (gmt 0)

10+ Year Member



I have some pages in Google which start with the IP address (not the URL) and this presents a problem with duplicate pages.

If any of the examples below are typed in then they all go to http://www.example.com

Options +FollowSymLinks
RewriteEngine on
# Redirect all requests for listed non-canonical domains to same page in www.example.com
RewriteCond %{HTTP_HOST} ^example\.com [OR]
RewriteCond %{HTTP_HOST} ^127\.0\.0\.1 [OR]
RewriteCond %{HTTP_HOST} ^dns1.example\.com [OR]
RewriteCond %{HTTP_HOST} ^dns2.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

The problem is with the following:-

http://127.0.0.1/acatalog/album/Trentan%20Timber%20Buildings/
should be http://example.com/acatalog/album/Trentan%20Timber%20Buildings/

Normally a redirect as follows would redirect all pages to the correct folders:-
RewriteEngine on
RewriteBase /
RewriteRule ^acatalog/album/(.*) http://www.example.com/acatalog/album/ [R=301,L]
but this does not work. Is this because of the IP address at being the start of the URL?

I've also tried this, as well as other combinations, and does not work:-

RewriteEngine On
RewriteCond %{HTTP_HOST} ^127.0.0.1/acatalog/album/$
RewriteRule (.*) http://www.example.com/acatalog/album$1 [R=301,L]

Can anyone help with this problem? I can't seem to find anything about redirecting folders from an IP address to the correct URL address which makes sense or works for me. Thank you.

[edited by: incrediBILL at 9:18 am (utc) on Aug 28, 2012]
[edit reason] IP removed, no specifics please, unlinked URL [/edit]

g1smd

8:48 pm on Aug 26, 2012 (gmt 0)

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



HTTP_HOST should be ONLY the hostname not any path information.

Escape all literal periods in every RegEx pattern.

You'll likely need to add a "/" before the "$1".

With no HTTP_HOST condition in one of your rules, you'll have an infinite redirect loop when the correct URL for the page is requested.

lucy24

9:26 pm on Aug 26, 2012 (gmt 0)

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



To canonicalize the host you need one condition, not dozens: "If the requested host IS NOT exactly {form I want} or exactly nothing..."

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$

and that's all. The "exactly nothing" option takes care of HTTP/1.0 requests.

Now can we talk about those literal spaces in your URLs? BIG problem. There should be nothing but alphanumerics, hyphens and lowlines. If your name is apache.org* you can use literal periods, but normal people shouldn't.


* Can you tell I got pretty worked up over this accidental discovery? :)

robinantill

9:59 pm on Aug 26, 2012 (gmt 0)

10+ Year Member



Hi Lucy24, thanks for your reply.
I did try your suggestion as follows:-

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$

but this does not do anything on my site. It might be something to do with hosting, I don't know, but the following are not canonicalized:
example.com
dns1.example.com
dns2.example.com
127.0.0.1

I agree with your point about the spaces in the URL's but my son set up our photo gallery and did not realise. Something we may need to look at as a separate issue.

Hi g1smd,
Thanks for your reply. I tried the following (with no other redirects in place as well as with other redirects in place):-

RewriteCond %{HTTP_HOST} ^127\.0\.0\.1/$
RewriteRule (.*) http://www.example.com/acatalog/album/$1 [R=301,L]

Didn't work for me unfortunately.

Does anyone offer a service to write an .htaccess file which would sort these issues out? This might be the best thing for me as it seems quite involved.

Thanks again,
Robin

[edited by: incrediBILL at 9:19 am (utc) on Aug 28, 2012]
[edit reason] IP removed, no specifics please, unlinked URL [/edit]

robinantill

10:04 am on Aug 27, 2012 (gmt 0)

10+ Year Member



Hi,
I found where my main problem was. Besides the main .htaccess file in my root directory I also had an .htaccess file in my http://www.example.com/acatalog/album/ directory. This created a conflict and since I removed the .htaccess it seems to be fine. Thanks.
Robin

g1smd

6:47 pm on Aug 27, 2012 (gmt 0)

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



The HTTP_HOST entry should not contain a trailing slash.

robinantill

6:59 pm on Aug 27, 2012 (gmt 0)

10+ Year Member



Thanks for that. Bit I did not need that in the code in the end.