Home > Back-end >  Regex redirect url to new domain through htaccess
Regex redirect url to new domain through htaccess

Time:03-11

I am trying to redirect all url with specific word to a new domain via htaccess. Here are the few url structure:

http://birmingham-printer-repairs.co.uk/brother/printer/repairs/in/abbots-langley/
http://birmingham-printer-repairs.co.uk/brother/printer/repairs/in/accrington/
http://birmingham-printer-repairs.co.uk/sharp/printer/repairs/in/york/

I need all url that contains (word printer/repairs/in) should be redirected to new domain

Here is my htaccess of wordpress

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^\/printer\/repairs\/in(.*)$ http://www.newdomain.com/ [R=301,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

CodePudding user response:

You can use the following :

Remember : this should be at the top of your htaccess or before other directives

RewriteEngine On

RewriteRule printer/repairs/in https://example.com [L,R]

If both domains (new and old) are pointing to the same document root , then use this

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteRule printer/repairs/in https://example.com [L,R]
  • Related