Home > database >  Rewrite multiple occurence of a term in htaccess
Rewrite multiple occurence of a term in htaccess

Time:03-23

I'm trying to convert all the & in my URL to & :

Here is my URL : https://test.com/index.php?option=com_media&task=file.upload&tmpl=component&820470bdbcec556c83a51cb33ca92922=kfp1tif94iuetenuab6hfi5mvd&690012fb504cea7eee8475c0963a8fc4=1&asset=image&format=json

and here is what I try to achieve :

https://test.com/index.php?option=com_media&task=file.upload&tmpl=component&820470bdbcec556c83a51cb33ca92922=kfp1tif94iuetenuab6hfi5mvd&690012fb504cea7eee8475c0963a8fc4=1&asset=image&format=json

So far I've tried the following rules :

RewriteEngine On
RewriteCond %{QUERY_STRING} (.*)(&)(.*)
RewriteRule ^.*$ %{REQUEST_URI}?%1&%3 [N,L,R=301]

But it's only replacing the last &

Thanks a lot for your help

CodePudding user response:

You can use these 2 rules for replacing all & with &. Note that there will only single redirect in the end:

RewriteEngine On

# recursively replace & with &
RewriteCond %{QUERY_STRING} (.*)&(.*) [NC]
RewriteRule ^ %{REQUEST_URI}?%1&%2 [NC,DPI,E=done:1]

# redirect in the end
RewriteCond %{ENV:done} =1
RewriteCond %{QUERY_STRING} !& [NC]
RewriteRule ^ %{REQUEST_URI} [R=301,NE,L]
  • Related