.htaccess redirect all pages to new domain

前端 未结 18 2282
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 14:47

Which redirect rule would I use to redirect all pages under olddomain.example to be redirected to newdomain.example?

The site has a totally

相关标签:
18条回答
  • 2020-11-22 15:30

    The below answer could potentially cause an infinite redirect loop...

    Here, this one redirects everything after the domain name on the URL to the exact same copy on the new domain URL:

    RewriteEngine on 
    RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
    
    www.example.net/somepage.html?var=foo
    

    redirects to:

    www.newdomain.com/somepage.html?var=foo
    
    0 讨论(0)
  • 2020-11-22 15:30

    From the usability point of view it would be better, if you also send the path with the request (i.e., what you have at the moment) and let your new site deal with it:

    You searched for "/products".

    Unfortunately this page is gone. Would you like to visit "/new_products" instead?

    (and better, still, doing this automatically.)

    This is obviously a lot of coding and heuristics for a larger website, but in my opinion it would pay off in terms of user satisfaction (when your carefully saved bookmark of your dream product just leads you to the front page of newdomain.com, this is frustrating.)

    0 讨论(0)
  • 2020-11-22 15:31

    There are various ways to do this and various redirects, I've listed them below:

    301 (Permanent) Redirect: Point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain:

    # This allows you to redirect your entire website to any other domain
    Redirect 301 / http://example.com/
    

    302 (Temporary) Redirect: Point an entire site to a different temporary URL. This is useful for SEO purposes when you have a temporary landing page and plan to switch back to your main landing page at a later date:

    # This allows you to redirect your entire website to any other domain
    Redirect 302 / http://example.com/
    

    Redirect index.html to a specific subfolder:

    # This allows you to redirect index.html to a specific subfolder
    Redirect /index.html http://example.com/newdirectory/
    

    Redirect an old file to a new file path:

    # Redirect old file path to new file path
    Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html
    

    Redirect to a specific index page:

    # Provide Specific Index Page (Set the default handler)
    DirectoryIndex index.html
    
    0 讨论(0)
  • 2020-11-22 15:31

    This is a bug in older versions of apache (and thus mod_rewrite) where the path prefix was appended to the rewritten path if it got changed. See here

    I think it was fixed in apache2 V2.2.12, there is a special flag you need to use which i will add here when i find it, (i think it was NP for No Path)

    RewriteRule ^(.*)$ http://newdomain.com/ [??]
    
    0 讨论(0)
  • 2020-11-22 15:33

    Just to clarify, after removing the hosting redirect which was in the way, my original solution also works:

    RewriteEngine On
    RewriteRule ^(.*)$ http://newdomain.com/ [R=301]
    
    0 讨论(0)
  • 2020-11-22 15:33

    If the new domain you are redirecting your old site to is on a diffrent host, you can simply use a Redirect

    Redirect 301 / http://newdomain.com
    

    This will redirect all requests from olddomain to the newdomain .

    Redirect directive will not work or may cause a Redirect loop if your newdomain and olddomain both are on same host, in that case you'll need to use mod-rewrite to redirect based on the requested host header.

    RewriteEngine on
    
    
    RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
    RewriteRule ^ http://newdomain.com%{REQUEST_URI} [NE,L,R] 
    
    0 讨论(0)
提交回复
热议问题