skip to Main Content

I’m trying to redirect http requests that contain a specific URI to a different domain with a different URI completely. Redirecting the top level domain works but I can’t seem to get the URI rules to redirect.

In essence it should act as follows:

If the url request is:

www.example.com/unique-URI

it needs to redirect to:

https://example2.com/anotheruniqueURI

Currently I have this:

  RewriteEngine On

  #This redirect works successfully
  RewriteCond %{HTTP_HOST} ^www.example.com$
  RewriteRule ^(.*)$ http://example2.com/something [R=301,L]

  #This attempt to redirect requests with the specific URI does not work.
  RewriteCond %{HTTP_HOST} ^www.example.com
  RewriteCond %{REQUEST_URI} ^/cars-application$ [NC]
  RewriteRule ^/(.*)$ https://example2.com/anotherURI/ [R=301,NC,L]

I’ve tried many different combinations inside my RewriteRule such as explicitly stating the URI like I did in the RewriteCond above but that doesn’t work. Using $1 here won’t apply since I’m redirecting to a completely different domain and URI. The URI’s I am expecting will be unique. Could you guys provide me some pointers. Is my regex correct or is my rewrite rule capture just wrong?

2

Answers


  1. Assuming you are redirecting from within a virtualhost of the first domain, you may just do the following:

    Redirect permanent /unique-URI http://www.domain2.com/newlocation 
    
    Login or Signup to reply.
  2. Your rule failed to work due to the leading slash in your RewriteRule’s pattern . Remove the slash to fix it.

    RewriteRule ^(.*)$ https://example2.com/anotherURI/ [R=301,NC,L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search