Mobile app version of vmapp.org
Login or Join
Berryessa370

: How to setup htaccess to redirect both IP and www.domain.com to domain.com I found many different htaccess examples showing how to redirect from www version to non-www version and vice versa,

@Berryessa370

Posted in: #Domains #Htaccess #IpAddress #Redirects

I found many different htaccess examples showing how to redirect from www version to non-www version and vice versa, but I can't just find a solution to redirect IP and www-version to non-www one.

RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^/?$ "http://example.com/" [R=301,L]

RewriteCond %{HTTP_HOST} ^XXX.XXX.XXX.XXX
RewriteRule (.*) example.com/ [R=301,L]


I've tried this but it doesn't work for IP redirection.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Berryessa370

1 Comments

Sorted by latest first Latest Oldest Best

 

@Kaufman445

Point #1 :

You must make sure that the hostname or IP address you are trying to redirect is already pointing to the web-host & location where you have the .htaccess file. If they are being pointing somewhere else, then no matter what you put in .htaccess, there will be no result.

Point #2 :

Redirects are generally cached by browsers. So while testing with different redirect rules, you must clear browser cache every time you try a different rule.

Point #3 :

The CODE you've posted should work if you've followed points #1 and #2 correctly. So most likely you are doing something wrong there.

Point #4: (bonus)

The easiest trick is to use the not (!) operator in RewriteCond.

The best thing about the not operator is that it'll redirect any accessible IP or hostname to your target/canonical hostname. So if you have multiple domain / IP address / sub-domain pointing to your desired host, all of them will be redirected to the desired host. The .htaccess CODE will be something like:

RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.*)$ "http://example.com/" [R=301,L]


This way, not only example.com, but also anything-else.example.com will be redirected to example.com (if both were pointing to the same host configuration).

Similarly, any IP address pointing to that same host+location will be redirected to example.com.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme