Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite QSA

How does Query String Append Work?

         

j_a_c_k

4:12 pm on Aug 1, 2006 (gmt 0)

10+ Year Member



Given an .htaccess like this
RewriteEngine on
RewriteRule .* dodah.php
RewriteRule .* a=1 [QSA]

and a request like this
[mysite.com...]
Having read the cryptic mod rewrite documentation at
[httpd.apache.org...]

I expected to execute the sript at dodah.php
with a query string of a=1

Instead I get an error
The requested URL /a=1 was not found on this server

So obviously QSA has little to do with the Query String and instead simply replaces the url as if the QSA were not there.

ChadSEO

4:52 pm on Aug 1, 2006 (gmt 0)

10+ Year Member



j_a_c_k,

QSA appends the original query string to the end of the rewritten URL. So if you requested http://www.mysite.com/a1.htm?myvar=true, and had the following rules:

RewriteEngine On
RewriteRule .* dodah.php [QSA]

Then the rewritten URL would be http://www.mysite.com/dodah.php?myvar=true

If you are simply wanting to append a query string, you could do so like this:

RewriteEngine On
RewriteRule (.*) $1?a=1

Chad

j_a_c_k

6:49 pm on Aug 1, 2006 (gmt 0)

10+ Year Member



Chad,
I understand what you say, although I did not see it from the documentation.

Does this mean that there really is no way to construct a query string piece by piece? appending this and that as we glean them from the rules?

Jack

ChadSEO

7:21 pm on Aug 1, 2006 (gmt 0)

10+ Year Member



j_a_c_k,

You can definitely do that. For instance, I could build a query string of "A=1&B=2&C=3" with the following rules:

RewriteEngine On
RewriteRule .* dodah.php?A=1
RewriteRule .* dodah.php?B=2 [QSA]
RewriteRule .* dodah.php?C=3 [QSA]

One this to remember is, each rewrite changes the request. So, let's say I want to rewrite everything to dodah.php, and append "t=1" to any URL with the string "test" in it, and "type=htm" to any URL that ends with htm. The following would not work, because it would never match the test or htm:

RewriteEngine On
RewriteRule .* dodah.php
RewriteRule test dodah.php?t=1 [QSA]
RewriteRule htm$ dodah.php?type=htm [QSA]

Instead, you have to look at THE_REQUEST variable, like this:

RewriteEngine On
RewriteRule . dodah.php
RewriteCond %{THE_REQUEST} ^(GET¦HEAD¦POST)\ .*test [NC]
RewriteRule . dodah.php?t=1 [QSA]
RewriteCond %{THE_REQUEST} ^(GET¦HEAD¦POST)\ .*htm$ [NC]
RewriteRule . dodah.php?type=htm [QSA]

Keep in mind that the vertical bars should be pipes (WebmasterWorld rewrites them). Hope that helps.

Chad

j_a_c_k

12:35 am on Aug 2, 2006 (gmt 0)

10+ Year Member



This has been a big help.
I think I can sell a variation of my design to the boss.

my new URL will look like this:
www.mysite.com/-a1-b2-modelpage.htm

I added the - in front of the parameter to make sure they didn't match on just any a-e followed by a number as in page1.htm

The generated URL should be:
www.mysite.com/dodah.php?a=1,b=2,page=modelpage.htm

But alas I get:
www.mysite.com/dodah.php?a=1&b=2&page=modelpage.htm

Which is not too bad considering that I did nothing to insert the ampersands or commas.

I am not sure from where the ampersands came but I need to make them int commas.

I have the following code in .htaccess
and it almost works, except that it puts an & between the output parameters where I would like a comma.

After three full days of mod rewrite my head is swimming, I cannot even figure out how to change the Ampersands to commas?


Will you help just once more?

====================================================
RewriteEngine on
RewriteRule . dodah.php?

RewriteCond %{THE_REQUEST} ^(GET劣EAD同OST).*-([a-z][a-z0-9]+\.htm)
RewriteRule . dodah.php?page=%2

RewriteCond %{THE_REQUEST} ^(GET劣EAD同OST).*-([a-z][a-z0-9]+\.html)
RewriteRule . dodah.php?page=%2

RewriteCond %{THE_REQUEST} ^(GET劣EAD同OST).*-f([0-9]+) [NC]
RewriteRule . dodah.php?f=%2 [QSA]

RewriteCond %{THE_REQUEST} ^(GET劣EAD同OST).*-e([0-9]+) [NC]
RewriteRule . dodah.php?e=%2 [QSA]

RewriteCond %{THE_REQUEST} ^(GET劣EAD同OST).*-d([0-9]+) [NC]
RewriteRule . dodah.php?d=%2 [QSA]

RewriteCond %{THE_REQUEST} ^(GET劣EAD同OST).*-c([0-9]+) [NC]
RewriteRule . dodah.php?c=%2 [QSA]

RewriteCond %{THE_REQUEST} ^(GET劣EAD同OST).*-b([0-9]+) [NC]
RewriteRule . dodah.php?b=%2 [QSA]

RewriteCond %{THE_REQUEST} ^(GET劣EAD同OST).*-a([0-9]+) [NC]
RewriteRule . dodah.php?a=%2 [QSA]