Forum Moderators: phranque

Message Too Old, No Replies

Forcing to www.

But what about 192.168.0.1?

         

ahmedtheking

5:11 pm on Jan 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm forcing viewers of my web site from mysite.com to www.mysite.com (im not blocking mysite.com, just directing them to www) for SSL reasons. However, locally, when I access the site via 192.168.0.1, I'm forced to, you guessed it, www.192.168.0.1! How can I stop this?

LifeinAsia

5:30 pm on Jan 4, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Check the server name when doing the redirect. If the servername is the IP address, don't do the redirect.

ahmedtheking

6:23 pm on Jan 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How can I do that in htaccess?

jdMorgan

6:34 pm on Jan 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post the relevant code so we can look at it.

Jim

ahmedtheking

10:27 pm on Jan 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, here it is:

RewriteRule ^account(.*)?$ [%{HTTP_HOST}...] [QSA,L]
RewriteRule ^contact(.*)?$ [%{HTTP_HOST}...] [QSA,L]
RewriteRule ^phpmyadmin(.*)?$ [%{HTTP_HOST}...] [QSA,L]

phranque

12:30 am on Jan 5, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



maybe you could add a RewriteCond to prevent implementation of this RewriteRule when the HTTP_HOST is numeric.
(i.e. a pattern that looks like an ip address)

and i'm sure jim will have a say about that ambiguous, greedy, and promiscuous ".*" usage.
=8)

jdMorgan

12:46 am on Jan 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond ${HTTP_HOST} !^192.168.0.1$
RewriteRule ^((account¦contact¦phpmyadmin).*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Replace all broken pipe "¦" characters in the code below with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

[edited by: jdMorgan at 2:12 am (utc) on Jan. 5, 2007]

ahmedtheking

9:37 am on Jan 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll try that out!

ahmedtheking

9:44 am on Jan 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Worked, but had to tweek it, here's the revised edition:

RewriteCond %{HTTP_HOST}!^192\.168\.1\.11$
RewriteRule ^((account¦contact¦phpmyadmin).*)$ [%{HTTP_HOST}...] [R=301,QSA,L]
RewriteCond %{HTTP_HOST} ^192\.168\.1\.11$
RewriteRule ^((account¦contact¦phpmyadmin).*)$ [%{HTTP_HOST}...] [R=301,QSA,L]

Cheers everyone!

jdMorgan

5:01 pm on Jan 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you'd like to shorten/simplify it a bit:

# If local IP
RewriteCond %{HTTP_HOST} ^192\.168\.1\.11$
RewriteRule ^((account¦contact¦phpmyadmin).*)$ https://192.168.0.1/$1 [R=301,L]
# Else not local
RewriteRule ^((account¦contact¦phpmyadmin).*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Here, we redirect to [192.....] if the request is from 192..
Otherwise, the request must not be from 192.., so we can switch to the [....] server without having to check the HTTP_HOST again.

Note that neither this nor the code in the previous posts would work if the both HTTP and HTTPS servers were in the same host container; In that case, it would be necessary to check the HTTPS/HTTP status for both rules before redirecting in order to avoid an 'infinite' redirection loop from HTTPS to HTTPS.

There is no need to use [QSA] unless you are Appending something new to the existing Query String. If you don't append something new, then the original query is passed through the rules unchanged by default.

Jim

ahmedtheking

5:15 pm on Jan 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh I thought QSA passes the query sent to, say, bool.php?lalalala to newbool.php?lalalala through .htaccess?

jdMorgan

7:32 pm on Jan 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes and no. But this passing-through of the query is the default behaviour of RewriteRule. You have to take steps to stop it from happening if you *don't* want to pass the query string.

[QSA] is only needed if you want to Append more query name/value pairs to a URL with a pre-existing query string using RewriteRule.

In the case of the code above, [QSA] does nothing but waste disk space and CPU time. It will work exactly the same with or without [QSA].

See the description of [QSA] in the Flags section of the mod_rewrite documentation, following the description of RewriteRule.

Jim

ahmedtheking

12:29 am on Jan 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I get it, so passing bool.php?lalala to newbool.php?lalala&$1 (to include the vars of the 1st one)

jdMorgan

1:20 am on Jan 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Replace original query with new query
Requested URL: example.com/bool.php?fa&la
Rule: RewriteRule ^bool\.php$ /newbool.php?na [L]
Resulting URL: example.com/newbool.php?na

Append new query to original query
Requested URL: example.com/bool.php?fa&la
Rule: RewriteRule ^bool\.php$ /newbool.php?na [QSA,L]
Resulting URL: example.com/newbool.php?fa&la&na

Remove original query
Requested URL: example.com/bool.php?fa&la
Rule: RewriteRule ^bool\.php$ /newbool.php? [L]
Resulting URL: example.com/newbool.php

Retain original query (default behaviour)
Requested URL: example.com/bool.php?fa&la
Rule: RewriteRule ^bool\.php$ /newbool.php [L]
Resulting URL: example.com/newbool.php?fa&la

Jim

ahmedtheking

11:28 am on Jan 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cool beans man, thanks!