Ad

.htaccess RewriteRule For Abc/xyz/myfile?s=t

- 1 answer

I'm a newb. I want a rewrite rule that translates URLs on the form:

abc/xyz/myfile?s=t

to

index.php?id=abc&a=xyz&b=myfile.xml&s=t

abc is a fixed prefix. xyz, myfile and the query string are back referenced.

This is the .htaccess file:

RewriteEngine On
RewriteRule ^abc/([_0-9a-z]+)/([_0-9a-z]+)\?(.+)$ index.php?id=abc&a=$1&b=$2.xml&$3 [NC]

It doesn't work. I debugged the rewrite rule with the following perl script:

#!perl
use strict;
use autodie;
$_ = "abc/xyz/myfile?s=t";
if ( /^abc\/([_0-9a-z]+)\/([_0-9a-z]+)\?(.+)$/ ) {
    print "index.php?id=abc&a=$1&b=$2.xml&$3";
}

The output is:

index.php?id=abc&a=xyz&b=myfile.xml&s=t

Why doesn't the .htaccess file work?

Edit: Seems like the last parameter (s=t) in the result URL is ignored.

Ad

Answer

You need to use:

RewriteEngine On
RewriteCond %{QUERY_STRING}  ^(.+)$
RewriteRule ^abc/([_0-9a-z]+)/([_0-9a-z]+)$ index.php?id=abc&a=$1&b=$2.xml&%1 [NC]

Because the query string is not part of the rewriterule left url.

Ad
source: stackoverflow.com
Ad