3

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

http://www.newexample.com

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. – Stephen Ostermiller Dec 5 '19 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 '19 at 15:32
  • Do the sites share a document root on the new server? – Stephen Ostermiller Dec 5 '19 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? – Stephen Ostermiller Dec 5 '19 at 15:35
  • 1
    No, I specifically don't want to reserve path. I want everything to redirect to main domain – AndyTTT Dec 5 '19 at 20:14
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.d­oma­in.com$ [NC]
Rewrit­eRule ^(.*)$ http:/­/ww­w.d­oma­in2.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.

1

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, privacy policy and cookie policy

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