I don’t know how useful it will be for you but still I’m sharing this information with you all.
How to set a variable in .htaccess file which you have to write so many times and it changes when it goes to development and then on production as well.
In my case,
I have a domain http://abc.example.com which needs to be redirected to http://www.xyz.com/
So my Rules are
RewriteRule ^secu/?$ http://www.xyz.com/security/ [L,R=301]
RewriteRule ^secu/archives.php/(.*)$ http://www.xyz.com/security/archive/$1 [L,R=301]
RewriteRule ^cnews/?$ http://www.xyz.com/news/ [L,R=301]
RewriteRule ^cnews/archives.php/(.*)$ http://www.xyz.com/news/archive/$1 [L,R=301]
#Setting Variable for Host Name as per the Development Environment
RewriteCond %{HTTP_HOST} ^abc.example.local.com$ [NC]
RewriteRule .* - [E=host_name:xyz.local.com]
RewriteCond %{HTTP_HOST} ^iabc.example.com$ [NC]
RewriteRule .* - [E=host_name:xyz.com]
RewriteRule ^secu/?$ http://%{ENV:host_name}/security/ [L,R=301]
RewriteRule ^secu/archives.php/(.*)$ http://%{ENV:host_name}/security/archive/$1 [L,R=301]
RewriteRule ^cnews/?$ http://%{ENV:host_name}/news/ [L,R=301]
RewriteRule ^cnews/archives.php/(.*)$ http://%{ENV:host_name}/news/archive/$1 [L,R=301]
As you can see in above example, now you don’t have to worry which environment you are working your rules will always work.
You can also set the variable without any condition and access it directly as mentioned below.
RewriteRule .* - [E=foo:bar]
%{ENV:foo}
posted by Kailash Yadav in
.htaccess,
Open Source and have
No Comments