Search code examples
.htaccess

Redirect all old domain pages to the same new domain pages


I'm struggling with .htaccess...

I'd like to redirect my old site this way: old.site to new.site old.site/a/ to new.site/a/ old.site/b/ to new.site/b/ etc...

But I'm getting old.site to new.site old.site/a/ to new.site old.site/b/ to new.site etc...

Thank you in advance.

EDIT

The two domains are pointing to different servers.

This is the complete .htaccess

# # av:php5-engine
AddHandler av-php8 .php

# # av:Toolbar
SetEnv AV_TOOLBAR 0

# av:AntiHotlink
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} \.(gif|jpe?g|png)$ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://([a-z0-9\-\.]*)distopia\.altervista\.org
RewriteCond %{REQUEST_URI} !^\/_altervista_ht\/
RewriteCond %{HTTP_REFERER} !^http://([a-z0-9\-\.]+)google\.
RewriteRule .*$ http://hl.altervista.org/split.php?http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# AntiHotlink

# av:PHP-upload
php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_input_time 300
# PHP-upload

RewriteEngine On
RewriteRule ^(.*)$ https://distopia-eva.org/$1 [R=301]

Solution

  • If the rule in question is the one at the end of the .htaccess file you've posted then there doesn't seem to be anything wrong with it. The problem would seem to be somewhere else. You should check the browser dev tools to see exactly what redirect(s) you are seeing (and from where). Clear your browser (and any intermediary) cache, since erroneous 301 (permanent) redirects are cached persistently by the browser.

    However, if the two domains point to different servers, as you've stated in your update, and you are redirecting everything like-for-like then you should delete everything in your .htaccess file and use just a simple (mod_alias) Redirect directive instead. For example:

    Redirect 301 / https://new.site/
    

    The Redirect directive uses simple prefix matching and everything after the match is copied onto the end of the target URL. So this directive will redirect /anything to https://new.site/anything and /foo/bar/baz?123 to https://new.site/foo/bar/baz?123.

    NB: Test first with a 302 (temporary) redirect to avoid potential caching issues.

    Reference: