Search code examples
apachemod-rewriteurl-rewriting

Redirect domain based on regular expression


I need to change domains when I have a certain path on the URL request. More specifically I need to identify first if a certain path (/path) exists on the URL of the request.

If it exists then show the website with the domain www.mynewdomain.com/path/. If /path does not exist on the URL of the request then show www.myolddomain.com or whatever the request will be.


Solution

  • You can use RewriteRule to redirect the request to the new domain. You can mention the config in the virtual host.

    RewriteEngine On
    RewriteRule /path(.*) http://www.mynewdomain.com/$1 [L,R]
    

    This configuration retains the URL part after the /path token [ denoted by (.*) ] and is rewritten to the redirected path using $1.