Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
This solution is simple and effective –  Sigurdur Jan 27 '12 at 8:53
add comment

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:

Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?

share|improve this answer
    
I already did try this and it did not work –  Sigurdur Jan 27 '12 at 8:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.