regex - htaccess [L] flag didn't work -


i have .htaccess file inside searchengine folder inside htdocs of xampps

    rewriteengine on     rewriterule ^\/?test$ view/index.php [nc,l]     rewriterule ^(.*)$   http://google.com [nc,l] 

then type in address bar:

  • localhost/searchengine/test

why redirect

not

  • view/index.php?

i thought must redirected

  • view/index.php

not

it because mod_rewrite runs in loop. first rule doing this:

starting url:

/searchengine/test 

ending url:

/searchengine/view/index.php 

now l flag causes mod_rewrite loop again. starting url becomes:

starting url:

/searchengine/view/index.php 

now 1st rule' pattern ^test doesn't match 2nd rule using .* matching pattern hence redirects google.

you can have rules follows prevent behavior:

rewriteengine on  rewriterule ^/?test$ view/index.php [nc,l]  rewritecond %{env:redirect_status} ^$ rewriterule ^ http://google.com [r,l] 

2nd rule has additional condition:

rewritecond %{env:redirect_status} ^$ 

redirect_status set 200 after first rule's execution. 2nd rule redirect if first rule hasn't executed first hence skip /test.

if you're using apache 2.4+ can use end instead of l end execution:

rewriteengine on  rewriterule ^/?test$ view/index.php [nc,end]  rewriterule ^ http://google.com [r,l] 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -