简体   繁体   中英

Apache Redirect URI Requests

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?

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 

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM