Writing if condition in htaccess file for URL rewriting

My htaccess code for url rewriting is as follows

RewriteEngine On
RewriteBase "/myekc/"
RewriteCond %{REQUEST_URI} ^/myekc/
RewriteCond %{REQUEST_FILENAME} !/(css|images|js)/ 
RewriteRule "/t/pepsi/(.*)$" "http://192.168.0.175:68/$1" [P]
RewriteRule "^(.*(?:xampp|htdocs)/)(.+)$" "http://192.168.0.179/myekc/t/pepsi/$2" [R,QSA,L]

      

This works for 1 URI. Now I want to write if condition in such a way as to write for multiple uri For example:

<If "req('uri') == '/t/pepsi/'">
RewriteBase "/myekc/"
RewriteCond %{REQUEST_URI} ^/myekc/
RewriteCond %{REQUEST_FILENAME} !/(css|images|js)/ 
RewriteRule "/t/pepsi/(.*)$" "http://192.168.0.175:68/$1" [P]
RewriteRule "^(.*(?:xampp|htdocs)/)(.+)$" "http://192.168.0.179/myekc/t/pepsi/$2" [R,QSA,L]
</if>
<If "req('uri') == '/t/coke/'">
RewriteBase "/myekc/"
RewriteCond %{REQUEST_URI} ^/myekc/
RewriteCond %{REQUEST_FILENAME} !/(css|images|js)/ 
RewriteRule "/t/coke/(.*)$" "http://192.168.0.175:68/$1" [P]
RewriteRule "^(.*(?:xampp|htdocs)/)(.+)$" "http://192.168.0.179/myekc/t/coke/$2" [R,QSA,L]
</if>

      

+3


source to share


1 answer


You don't need an explicit condition here <If>

. With mod_rewrite, if you need a directive to apply to a different url, you simply include that directive for a different url by specifying the appropriate pattern RewriteRule

. For example:

RewriteEngine On

RewriteRule ^(.*(?:xampp|htdocs)/)(.+)$ http://192.168.0.179/myekc/t/pepsi/$2 [R,L]

RewriteCond %{REQUEST_URI} ^/myekc/
RewriteCond %{REQUEST_FILENAME} !/(css|images|js)/ 
RewriteRule /t/pepsi/(.*)$ http://192.168.0.175:68/$1 [P]

RewriteCond %{REQUEST_URI} ^/myekc/
RewriteCond %{REQUEST_FILENAME} !/(css|images|js)/ 
RewriteRule /t/coke/(.*)$ http://192.168.0.175:68/$1 [P]

      

Note that the directive RewriteBase

can only be used once per file .htaccess

(the last instance wins and controls the entire file). But you are not using it at all in the directives you posted, so I removed them entirely.

However, these two rules are currently doing the same, so they can be combined:

RewriteCond %{REQUEST_URI} ^/myekc/
RewriteCond %{REQUEST_FILENAME} !/(css|images|js)/ 
RewriteRule /t/(?:pepsi|coke)/(.*)$ http://192.168.0.175:68/$1 [P]

      




I need an if condition because, I want to set a cookie value for each template. So the cookie value will be the paths "/ t / pepsi /" and "/ t / coke". If I use together, the last cookie value is taken into account. Example: you want to install Header set Set-Cookie "mycookie=/t/pepsi/" "expr=-z %{req:Cookie}"

and Header set Set-Cookie "mycookie=/t/coke/" "expr=-z %{req:Cookie}"

.

You still don't need an explicit condition <If>

, as mod_rewrite allows you to set cookies, which is probably easier to use in this case (since you're already using mod_rewrite to capture the path). For example:

RewriteRule (/t/(?:pepsi|coke)/)(.*)$ http://192.168.0.175:68/$1 [CO=test:$1:.example.com,P]

      

The CO

( cookie

) flag in the directive RewriteRule

sets the cookie test

(on the domain .example.com

) to the value of the URL path in the request (ie .. / t / pepsi / "or" / t / coke / ").

Help:
https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_co

0


source







All Articles