views:

396

answers:

3

How to construct a rule that checks if the incoming request is of certain terms, if no, then reroute to other URL?

For example, I have a website my.example.com. If the incoming URL is my.example.com/login then it should execute that request. If it's something else (not login), then the request must be reroute to your.example.com. For example,

  • my.example.com/login2 must reroute to your.example.com/login2
  • my.example.com/nologin must reroute to your.example.com/nologin
  • my.example.com/getname must reroute to your.example.com/getname

And of course, if it's

  • my.example.com/login must reroute to my.example.com/login

How to use RewriteRule and mod_rewrite to achieve this?

A: 
RewriteCond %{HTTP_HOST}   !^www\.myexample\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://www.myexample.com/$1 [L,R]
gacrux
I don't think your script does what is intended... for example, I don't see any logic involving login and yourexample.com?
Ngu Soon Hui
Ah ok, sorry i misunderstand the question first time round.
gacrux
A: 

I know this isn't exactly what you've asked for, but would do it by using .htaccess to direct all requests (valid or not) through to one file & then using server-side scripting (e.g. PHP in my case) to switch through the valid options, catching anything else with a default that redirects to the other URL.

e.g. this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php

sends anything that isn't a request for an actual file or directory to index.php

da5id
+4  A: 

Try this rule:

RewriteCond %{HTTP_HOST} ^my\.example\.com$
RewriteRule !^login$ http://your.example.com%{REQUEST_URI} [L,R=301]
Gumbo
I used example.com by purpose and so should you. See http://tools.ietf.org/html/rfc2606
Gumbo