+1 vote
in Web & Google by (74.2k points)

If the URL contains specific keywords, I want to redirect the page to a new domain. The redirection should include the full original URI, i.e., it should just change the hostname on the requested URL.

E.g.

original URL:

abc.com/hello/world/how-are-you

if the URL contains "hello", it should be redirected to

xyz.com/hello/world/how-are-you

How can I do it using the .htaccess file of the Apache server?

1 Answer

+1 vote
by (56.8k points)
selected by
 
Best answer

You can use "RewriteCond %{REQUEST_URI}" to check specific keywords in the URL and then "RewriteRule" to redirect the old URL to the new URL.

Here is an example:

RewriteEngine On

RewriteCond %{REQUEST_URI} hello [OR]

RewriteCond %{REQUEST_URI} tello

RewriteRule ^(.*)$ https://xyz.com%{REQUEST_URI} [L,R=301,NC]

The above example checks if "hello" or "tello" is present in the URL. If yes, it redirects the page to the new hostname xyz.com with the full original URI.


...