So I have domain http://example.com.

I want to setup htaccess to redirect all of these variations

  • http://www.example.com

  • http://example.com

  • https://example.com/111

  • http://example.com/222

... basically EVERYTHING on this domain I want redirect to


How to set it up properly in .htaccess file?

All domains on same server.

Path needs not to be kept.

  • How are the old and new sites set up? Are they on separate servers? Separate virtual hosts? Both mapped to the same document root directory? The mechanics of the redirect are going to differ depending on whether or not the sites share a document root. Dec 5, 2019 at 15:18
  • 1
    All sites on new server. I just need visitors of old site to be redirected to new site. Any url they enter on old site needs to redirect them to main url of new site.
    – AndyTTT
    Dec 5, 2019 at 15:32
  • Do the sites share a document root on the new server? Dec 5, 2019 at 15:33
  • When you say you want it to redirect to the new site, are you hoping to preserve the path portion of the URL. For example https://example.com/111 to https://newexample.com/111? Dec 5, 2019 at 15:35
  • 1
    No, I specifically don't want to reserve path. I want everything to redirect to main domain
    – AndyTTT
    Dec 5, 2019 at 20:14

2 Answers 2


This guide from Apache should help: http://httpd.apache.org/docs/current/rewrite/intro.html

But for something you can copy and paste, see this cheatsheet but drop the $1 from the Rewrite Rule.

It's easier for me to post the code than to explain:

RewriteEngine on
Rewrit­eCond %{HTTP­_HOST} ^www.example.com$ [NC]
Rewrit­eRule ^(.*)$ http:/­/ww­w.newexample.com/ [R=301,L]

If that doesn't work, then your hosting company is restricting the settings that you can set in .htaccess and you'll need to call support to get it straightened out.

  • Although this won't redirect EVERYTHING as stated in the question. It won't redirect example.com (non-www) for instance.
    – MrWhite
    Jan 29, 2020 at 18:16
  • Right, because the RewriteCond looks for the (old) domain prefixed by the www subdomain it wouldn't impact other subdomains or the naked domain...typically, the configuration rules dictating whether or not to use www are in a section of their own so I didn't delve into that. I try to avoid using .htaccess files for performance reasons but when I do, I use the HTML5 Boilerplate as a starting point, it includes configuration blocks for using www or not so a user can just comment out whichever is inapplicable.
    – adam-asdf
    Jan 30, 2020 at 20:20

If both of your domain is on same server, on each request your server needs to work twice, one for handling with htaccess, another for serving site from correct address/domain.

What if you could do this directly from domain provider.

Redirecting to new domain using DNS settings

Most of domian provider let's you redirect domain preserving url and with header 301 or whatever you wish to.

I recommend you check with your domain provider, go to olddomain.com's setting, instead setting up A record of the domian, choose redirect option. Enter newdomain.com and forward all query strong and path as well.

This will save tons of request from being hit on your server, and your DNS might cache new path automatically.

Check if you can implement this solution, would be really good.

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.