0

I have multiple URL's that looks something like this :

  • stage-app-platform-com.something.com
  • stage.something.com
  • stage1.something1.com

I wanted to create an Apache 301 redirect rule which, if the URL contains the pattern app-platform should forward to stage1.app.com and any other request hitting the server should be redirected to stage2.app.com. To do so, I wrote the following redirect rule:

RewriteRule ^stage-app-platform-com$ https://stage1.app.com/ [L,R=301]
RewriteRule ^(.*)$ https://stage2.app.com/ [L,R=301]

Now the second rule which states all the traffic hitting should be re-directed to https://stage2.app.com/ works but the first one doesn't seem to work. If the first URL is requested Apache forwards to the https://stage2.app.com/ instead of https://stage1.app.com/

As I am pretty much new to writing re-write rules, I am finding it a bit difficult. I would appreciate any help.

Spaniard89
  • 107
  • 1
  • 3
  • 7

2 Answers2

1

RewriteRule's don't see the hostname part of the URL. What you need to use is RewriteCond as well, something like:

RewriteCond "%{HTTP_HOST}" "^stage-app-platform-com\."
RewriteRule .* https://stage1.app.com/ [L,R=301]
RewriteRule .* https://stage2.app.com/ [L,R=301]

This causes the first RewriteRule to apply only if the RewriteCond condition matches. If not it should then fall through to the second RewriteRule.

The mod_rewrite documentation is here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

bodgit
  • 4,751
  • 16
  • 27
  • Thanks a ton for the reply, but can you please shed some light what these 3 lines of code are doing? – Spaniard89 Aug 11 '16 at 14:24
  • Somehow it doesn't seem to work. It is still forwarding everything to https://stage2.app.com/ – Spaniard89 Aug 11 '16 at 14:56
  • So follow http://httpd.apache.org/docs/current/mod/mod_rewrite.html#logging and capture the rewrite logs for a request to see what's going on and update your question. – bodgit Aug 11 '16 at 15:32
0

Try the following. But first explicity clear your browser cache. Redirects are often cached.

<if "%{HTTP_HOST} =~ /^stage-app-platform-com\./">
   Redirect permanent https://stage1.app.com/
</if>
<else>
   Redirect permanent https://stage2.app.com/
</else>
Unbeliever
  • 2,336
  • 1
  • 10
  • 19