0

I have the following path:

http://www.domain.com/cgi-bin/hsrun.exe/Distributed/Postphil/postphil.htx;start=DetectLanguage?Language=LANG_Icelandic

If the path contains the folder /postphil I want to redirect to a new domain

But I have not been able to get the RewriteCond rule to work

RewriteCond %{REQUEST_URI} ^postphil+ [NC]
RewriteRule ^(.*)$ https://www.newdomain.com/ [R=301,L] 

I have been using the on-line tester at rewrite-rule-tester to try figure out something that works, but no luck.

Any idea on how to write the RewriteCond condition?

ThinkingMonkey
  • 476
  • 1
  • 9
  • 17
Sigurdur
  • 103
  • 2

2 Answers2

1

Context can be important with rewrite rules. If your RewriteCond is in a .htaccess file or inside a <Directory > block, there is no leading slash on the URI. If it is in any other part of the Apache configuration, the URI will begin with a slash.

The reason for this is that in a directory context, the file path leading up to the directory in question is removed and that path always ends in a slash.

In your case, there is also all of this: /cgi-bin/hsrun.exe/Distributed/ before the Postphil bit but you require the URI to start with postphil so the RewriteCond will not match for that reason either.

The end of the regex ( l+ ) mean "one or more l" which is also a bit weird but since it does match a single 'l' that part should work.

If all you want is exactly what you said: "If the path contains the folder /postphil I want to redirect to a new domain" then this will do the trick, no need for a RewriteCond:

RewriteRule postphil https://www.newdomain.com/ [NC,R,L]

I used a 302 redirect rather than a 301 because browsers cache 301s. Even if you change the target of the redirect, if your browser has already seen a 301 for that URL it will just go straight there. You can change it to a 301 once you have finished testing and are happy that it works properly.

Ladadadada
  • 25,847
  • 7
  • 57
  • 90
0

Try this and tell me if it works:

RewriteCond %{REQUEST_URI} postphil [NC]
RewriteRule (.*) https://www.newdomain.com/$1 [R=301,L] 

Please read this:

Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod_Rewrite Rules but Were Afraid to Ask

Olivier Pons
  • 612
  • 1
  • 5
  • 21