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 (.*) http://example.com/$1 [R=301,L]

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

  • 1
    Is your site accessible by the IP address? If it is then the above should work. But your canonical www redirect is not a universal redirect, it only redirects the home page? (Aside: you don't need to escape colons, slashes and dots in the RewriteRule substitution.)
    – MrWhite
    Commented Jan 13, 2017 at 13:22
  • when I use this code in htaccess it redirects from www.domain.com to domain.com but doesn't redirect when I enter IP in web browser. About the second question - I have no idea.. I'm completely new to this topic, how can I find that?
    – Revn
    Commented Jan 13, 2017 at 13:26
  • Just need to clarify... "Is your site accessible by the IP address?" When you request http://<your-servers-ip-address>/ do you see your website? My other comment regarding your canonical www redirect is because it won't redirect http://www.example.com/<something>.
    – MrWhite
    Commented Jan 13, 2017 at 13:32
  • no it won't redirect if i put http://<website_ip_adress>/
    – Revn
    Commented Jan 13, 2017 at 15:29
  • 3
    Yes, you've already said it doesn't redirect, but can you access your website when you type your server's IP address? Or do you see "something else"? My point is, that if you are on a shared server that hosts multiple websites then you can't access your site by the IP address anyway because there will be 100s of sites on the same IP address? And if you can't access your website then you can't redirect it either. Please confirm this. Otherwise, the code you have posted should already work (as I stated above), which is why I am querying this.
    – MrWhite
    Commented Jan 13, 2017 at 15:46

1 Answer 1


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/$1" [R=301,L]

This way, not only www.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.

  • 1 - how can I learn that? If I use IP of website in FTP client instead of server adess it loads normally. 2. I know that, checked even on different browsers and nibbler says It doesn't redirect.
    – Revn
    Commented Jan 16, 2017 at 7:15
  • 1. Just keep a normal file in the same directory as the .htaccess file. If you can access that file with IP in browser, then that means the IP is pointing to the same hostname & redirect will work with .htaccess for IP. FTP works because in that case you are using a unique username, http protocol doesn't have that.
    – Fayaz
    Commented Jan 16, 2017 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.