Rewrite Rule - If the incoming request does not consist of certain characters, then rewrite it to a different url

How do I create a rule that checks if an incoming request has certain terms, if not, then redirect to a different URL?

For example, I have a website my.example.com

. If an incoming url my.example.com/login

, then it should fulfill this request. If it is something else (not login

), the request should be redirected to your.example.com

. For instance,

  • my.example.com/login2

    should redirect to your.example.com/login2

  • my.example.com/nologin

    should redirect to your.example.com/nologin

  • my.example.com/getname

    should redirect to your.example.com/getname

And, of course, if he

  • my.example.com/login

    should redirect to my.example.com/login

How do I use RewriteRule and mod_rewrite to achieve this?

0


a source to share


3 answers


Try the following rule:



RewriteCond %{HTTP_HOST} ^my\.example\.com$
RewriteRule !^login$ http://your.example.com%{REQUEST_URI} [L,R=301]

      

+4


a source


RewriteCond %{HTTP_HOST}   !^www\.myexample\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://www.myexample.com/$1 [L,R]

      



0


a source


I know this is not exactly what you asked for, but do it using .htaccess to route all requests (valid or not) to a single file and then use server side scripts (like PHP in my case) to switch valid parameters, intercepting everything else by default, which redirects to a different url.

eg. this is:

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

      

sends anything that is not a request for the actual file or directory index.php

0


a source







All Articles