Category: How to do

Looping / Recurring in Apache Mod Rewritting Rule

Mod rewrite , much useful module in apache httpd web server. Although, we utilize it for rewriting the url , redirection, I just want to point out about a special flag which acts like a while loop.

So let’s say, you have a requirement that you want to replace some character or want to process mod rewrite rule , just like a do while loop, but you do not have any control over, how many times it can appear in url.

Example:-

https://example.com/test_underscore_replace_by_hyphen

So you want to replace each of the underscore by hyphen.

https://example.com/test-underscore-replace-by-hyphen

This is achieved by N [Next] flag in rewrite condition.

RewriteEngine on
RewriteRule ^(.+)_(.+)$ $1-$2 [N] 

question:-  How to control, then number of loop iteration ?

RewriteEngine on
RewriteRule ^(.+)_(.+)$ $1-$2 [N=10] 
#It means , iteration will be 10 times.

Imp-1 :- If you do not specify default looping is 32000 .

Imp-2 :- Controlling iteration in directory path as well.

Let’s say url is https://example.com/dir_1/dir_2/test_underscore_replace_by_hyphen

if you test this url against the above rule, it will not produce the desired result.
if you check error log , you will find the
add path info postfix” line.

it means , mod_rewrite will iterate over request uri and search and it will add the path next to it. this will create a url that will not be valid.

Coming to point,
Add DPI [discardpath] flag

RewriteEngine on
RewriteRule ^(.+)_(.+)$ $1-$2 [N,DPI] 
#It means , iteration will be 10 times.

result will be 

https://example.com/dir-1/dir-2/test-underscore-replace-by-hyphen

how to make command aliases for command prompt?

use-case

let say there is one long command you often use, may be ssh to server with certificate ip and user

so either you go from history to connect or you type every time.

for history, that may be clean some time or you working on multiple terminal window at once and close them.

so what is the good alternative?

use alias , type once, and use every time from anywhere

like

1) command : /usr/bin/php7.0 -d memory_limit=-1 /var/www/html/bin/phar/php7cc.phar

either I type every time this command or can make an alias like

>>> /usr/bin/php7.0 -d memory_limit=-1 /var/www/html/bin/phar/php7cc.phar

make an entry in ~/.bash_aliases

alias php7cc='/usr/bin/php7.0 -d memory_limit=-1 /var/www/html/bin/phar/php7cc.phar'

reload it with

>>> source ~/.bashrc

now i just need to type php7cc inn command prompt to run this whole command

>>> php7cc

Note : if it is first time you are creating the .bash_aliases file,

then you may need to add the code in .bashrc to include .bash_aliases

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

2)     alias sdu=’du -hs * | sort -hr’

Example of .bash_alias file