.htaccess redirect all pages to new domain

前端 未结 18 2270
被撕碎了的回忆
被撕碎了的回忆 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:14

    I've used for my Wordpress blog this as .htaccess. It converts http://www.blah.example/asad, http://blah.example/asad, http://www.blah.example2/asad etc, to http://blah.example/asad Thanks to all other answers I figured this out.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} !^YOURDOMAIN\.example$ [NC]
    RewriteRule ^/(.*)$ https://YOURDOMAIN.example/$1 [R=301,L]
    
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    0 讨论(0)
  • 2020-11-22 15:16

    The previous answers did not work for me.

    I used this code. If you are using OSX make sure to use the correct format.

    Options +FollowSymLinks -MultiViews
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.)?OLDDOMAIN\.com$ [NC]
    RewriteRule (.*) http://www.NEWDOMAIN.com/ [R=301,L]
    
    0 讨论(0)
  • 2020-11-22 15:22

    Use conditional redirects with Options -FollowSymLinks and AllowOverride -Options disabled by the Hoster if a few local files should be served too:

    Sample .htaccess

    # Redirect everything except index.html to http://foo
    <FilesMatch "(?<!index\.html)$">
        Redirect 301 / http://foo/
    </FilesMatch>
    

    This example will serve local index.html and redirects all other staff to new domain.

    0 讨论(0)
  • I tried user968421's answer and the OP's solution but the browser popped up a security error for a checkout page. I can't tell you why exactly.

    Our host (Site Ground) couldn't figure it out either.

    The final solution was close, but a slight tweak to user968421's answer (side note: unlike the OP, I was trying to redirect to the corresponding page, not just to the homepage so I maintained the back reference [the $1 after the domain] from user968421's answer):

    RewriteEngine on
    RewriteRule (.*) https://newdomain.com/$1 [R=301,L]
    

    Got the tweak from this htaccess redirect generator recommended by a Host Gator article (desperate times, desperate measures, amiright?).

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

    My solution as SymLinks did not work on my server so I used an If in my PHP.

    function curPageURL() {
        $pageURL = 'http';
        if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
        } else {
            $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
        }
        return $pageURL;
    }
    $redirect = str_replace("www.", "", curPageURL());
    $remove_http_root    = str_replace('http://', '', $redirect);
    $split_url_array     = explode('/', $remove_http_root );
    
    
    if($split_url_array[0] == "olddomain.com"){
        header("Location: http://www.newdomain.com/$split_url_array[1]");
        die();
    }
    
    0 讨论(0)
  • 2020-11-22 15:29

    My reputation won't allow me to comment on an answer, but I just wanted to point out that the highest rated answer here has an error:

    RewriteEngine on 
    RewriteRule ^(.*)$ http://www.newdomain.com$1 [R=301,L]
    

    should have a slash before the $1, so

    RewriteEngine on 
    RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
    
    0 讨论(0)
提交回复
热议问题