301 redirect with wildcard in htaccess not working

181 Views Asked by At

All traffic will be directed from

https://oldsite.com/

to

https://www.newsite.com

I am looking for a 301 wildcard redirect solution for the following

https://oldsite.com/blog/*(the slug)

to

https://www.newsite.com/blog/*(matching slug)

Used htaccess file is:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>
1

There are 1 best solutions below

0
On

With your shown samples, please try following. Keep your domain redirection at the top of your htaccess file then keep other Rules. Also make sure to clear your browser cache before testing your URLs.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

##Have your domain changes rules here...
RewriteCond %{HTTP_HOST} ^oldsite\.com$ [NC]
RewriteRule ^  https://www.newsite.com%{REQUEST_URI} [NE,R=301,L]

RewriteRule ^index\.php$ - [L]

##Non existing files/directories rewrite rules here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]
</IfModule>