Title: .htaccess rewrite rule causes endless loop
Tags: apache;.htaccess;redirect;mod-rewrite;url-rewriting
Question: I want my .htaccess file to redirect to some page if any wildcard as a subdomain entry hit the browser.  i.e.  I want
sam.xyz.com
To redirect to
sam.xyz.com/view.php?id=sam
I am using following rewrite rules for redirect.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.xyz.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+).xyz.com
RewriteRule ^(.*)$      /view.php?id=%1 [L,R]
Problem i am facing is that it does not shift to new domain keeping query string instead it generates an endless loop
sam.xyz.com
redirects to
http://sam.xyz.com/view.php?id=sam
But doesnt move to url above without endless loop.
Kindly help me out.
Thanks in advance,

Here is the accepted answer: you should add a condition for redirect to prevent redirection loop:
RewriteCond %{REQUEST_URI} !^/view\.php
the whole code would be:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.xyz.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+).xyz.com
RewriteCond %{REQUEST_URI} !^/view\.php
RewriteRule ^(.*)$ /view.php?id=%1 [L,R]
Comment for this answer: oups , forgot a slash, try it now
Comment for this answer: yeh, just remove the R flag at the end of RewriteRule (replace [L,R] with [L]) ,  feel free to mark the answer as accepted :)
Comment for this answer: maybe i did not understand correctly, you wanna to rewrite sam.xyz.com to sam.xyz.com/view.php?id=sam without changing the url adress at the client browser ? if yes, then removing `R` from my answer should  do the trick (clear the browser cache)
Comment for this answer: just add '/sam' before '/view.php...' in the rewriterule.
Comment for this answer: page concatination solved but domain loses its query and cause endless loop.What i am doing wrong?
Comment for this answer: I have added the same above rules still the same loop
Comment for this answer: thanks that works :)
need one more help , how can i hide view.php?id=sam from the url keeping its value backend.
Comment for this answer: or atleast hide query string id=sam from address bar,can i?
Comment for this answer: Plus what if my view.php page locate in a directory as
xyz.com/sam/view.php what changes are required?
Help would be appreciated :)
Comment for this answer: what about having view.php in sub directory,what changes are required?
Comment for this answer: Thanks that works  ,  Can I just omit query string after view.php as sam.xyz.com/view.php  keeping query variable at back end..?

Here is another answer: You are redirecting to: prefix.domain.tld/view.php?id=prefix
Ensure that the url does not contain: id=prefix.
This solution prevents, that someone call's the url: aaa.example.com/view.php?id=bbb
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.xyz.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+).xyz.com
RewriteCond %75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0%{QUERY_STRING} !^([^:]+)75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0.*id=\1
RewriteRule ^ /view.php?id=%1 [L,R]
Note: (.*) in the rewrite rule is obsolete.
Leave the R away to do not redirect the visitor to the url (/view.php?id=%1)
Comment for this answer: That helps , how can I hide query variable from url?

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:06/27/13', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<apache><.htaccess><redirect><mod-rewrite><url-rewriting>', 'Title': '.htaccess rewrite rule causes endless loop', 'ViewCount': 4004, 'id': 17351608, 'user_AboutMe': '', 'user_AccountId': 2974547.0, 'user_DisplayName': 'salmanpervaiz', 'user_Reputation': 23.0, 'user_Views': 4.0, 'user_id': 2525888.0}}