Search code examples
apache.htaccessvariableshttp-redirectmalformed

Redirect a malformed URL with more than 1 variable


Redirect with "malformed double ?"

I try the following: an old URL that has to be kept alive is formed like this: /index.php?id=5041?var1=val1&var2=val2&var3=val3

I have to redirec it to something like this:

/path1/path2/?id=1234?var1=val1&var2=val2&var3=val3

whatever I try - it's either an infinite loop within index.php or it cuts of the rest of the vars after the id. The rest of the string should be simply attached.

ie.

#Only if id = 1234
RewriteCond %{QUERY_STRING} ^id=1234? [NC]
#get id for variable $1
RewriteCond %{QUERY_STRING} id=(\d+) [NC]
#get rest for variable $2
RewriteCond %{QUERY_STRING} val1=(\d+) [NC]
RewriteRule ^index\.php$ /path1/path2/?myid=%1&val2=%2

Solution

  •  RewriteEngine on
    RewriteCond %{THE_REQUEST} /index\.php\?id=([^?]+)\?var=([^&]+)&var2=([^&]+)&var3=([^\s]+) [NC]
    RewriteRule ^index\.php$ /path1/path2/ [QSA,NC,L,R]
    

    The condition above

    RewriteCond %{THE_REQUEST} /id=([^?]+)\?var=([^&]+)&var2=([^&]+)&var3=([^\s]+) [NC
    

    checks if /index.php has specific query strings ,redirect it to a new location.