Forum Moderators: phranque

Message Too Old, No Replies

Detect browser and redirect to page in same server

redirect

         

DilanWijerathne

9:55 am on Jun 18, 2015 (gmt 0)

10+ Year Member



I'm working on a codeIgniter project with using MVC.

I need to detect Microsoft Internet Explorer (IE) agents and redirect them to another page in my localhost using .htaccess file. Then I tried something like this:

RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

RewriteCond %{HTTP_USER_AGENT} "MSIE [6-8]" [NC]
RewriteRule .* http://localhost/secPay/doc [R]
But Unfortunately it does not work and gives error like below:

This webpage has a redirect loop
ERR_TOO_MANY_REDIRECTS
But it redirects to another servers like http://www.example.com

But I need to redirect it to http://localhost/secPay/doc

So what can I do? How to solve this?

[edited by: Ocean10000 at 3:58 pm (utc) on Jun 19, 2015]
[edit reason] examplified. [/edit]

SunnyCodes

8:28 am on Jun 22, 2015 (gmt 0)

10+ Year Member



It creates a loop because when it redirects to /secPay/doc, it tries to redirect again, hence the loop. You should exclude /secPay/doc (or whichever page on your site) with a rewrite condition, so it will not redirect again.

RewriteCond %{HTTP_USER_AGENT} "MSIE [6-8]" [NC]
RewriteCond %{REQUEST_URI} !^/secPay/doc
RewriteRule .* /secPay/doc [R]

lucy24

3:37 pm on Jun 22, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



But it redirects to another servers

You've told it to, by including a full protocol-plus-domain in the target. (Nobody has ever figured out why Apache's documentation appears to be wrong on this point.) Start the target with / for root to stay with the same host.

Incidentally, [R] does not imply [L]. You need to say it explicitly, unless you've got a reason to deliberately not specify [L]. So [R=301,L]. Obviously on local testing it doesn't "really" matter what kind of R it is, but it's good to stick with the habit.

DilanWijerathne

9:02 am on Jun 24, 2015 (gmt 0)

10+ Year Member



Dear SunnyCodes it does not work :( my complete htaccess file is

RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

RewriteCond %{HTTP_USER_AGENT} "MSIE [6-8]" [NC]
RewriteCond %{REQUEST_URI} !^/doc
RewriteRule .* /doc [R]

, how can I make this fix ?

DilanWijerathne

9:08 am on Jun 24, 2015 (gmt 0)

10+ Year Member



browser says a error like this

ERR_TOO_MANY_REDIRECTS

lucy24

4:54 pm on Jun 24, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



#1 what browser are you using for testing: MSIE as in the rule, or something else?
#1a why are you even using MSIE 8 when they're up to 11 by now?
#2 why is the rule creating an external [R] redirect located after the rule creating an internal rewrite alone?
#3 why does neither rule have the [L] flag?
#4 this cannot possibly be the entire htaccess from beginning to end. Did you mean that these are the only RewriteRules? What about other htaccess files and/or config containing other external redirects, such as via mod_alias?
#5 what does the browser's address bar say at the moment it puts up the Too Many Redirects error message?
#6 is "doc" the actual, literal filename or have you obfuscated it? It's OK to use real file and directory names, so long as all domains are recast as example.tld

I had a couple more questions but the Forums ate them.