IP to url htaccess rule?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

IP to url htaccess rule?

Post by eshban »

Hello,

My website is also accessed by my website ip address. I want that if any user trying to open my website by my website ip address, it will be automatically redirected to my website url.


Can anyone guide me about this.

Regards,
eshban
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: IP to url htaccess rule?

Post by John Cartwright »

Use a RewriteCond + REMOTE_HOST variable to apply redirection rules to a specify rule, i.e.,

Code: Select all

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_HOST} !^127\.0\.0\.1
//your redirection rule here
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: IP to url htaccess rule?

Post by twinedev »

Here is what I use. It says that if someone comes to the site not using www.example.com, redirect them to www.example.com, including the rest of the URL.

[text]RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.net [NC]
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L][/text]
Post Reply