Rewrite QSA, understanding it better - .htaccess

So, I'm having a difficult time understanding QSA. Since
RewriteRule ^search/(.*)$ search.php?s=$1 [NC,L,B,QSA]
RewriteRule ^search/(.*)$ search.php?s=$1 [NC,L,B]
Both give me the same result. I read that QSA appends any parameters passed (at least I think that's what I understood from it). However it's not exactly working for me.
Currently I have the url
http://localhost:8888/search/hey+i%27m+a+search+query&SortBy=day
which returns
hey i'm a search query&SortBy=day
I can set it to
RewriteRule ^search/(.*)&SortBy=(.*)$ search.php?s=$1&SortBy=$2 [NC,L,B,QSA]
which will successfully return the get parameter, but I from what I understand about the QSA, it should be automatically handled...right?
I got my information from here -
What does $1 [QSA,L] mean in my .htaccess file?
Basically, my question is, why should I use QSA? And what kind of benefits would it provide in this situation?
(Sorry for being blunt, but I can't get a good grasp of this)

The problem is that your example URL has no query string. You use ? to designate the query string:
http://localhost:8888/search/hey+i%27m+a+search+query?SortBy=day
The ?SortBy=day will be automatically appended to the rewritten query string through the QSA flag.

Related

.htaccess, virtual directories, and semi-complex URLs

I'm basically just trying to have a master syntax for predictable URLs. Simple URL is no problem
RewriteEngine on
# RewriteRule ^friendlyUrl/content/?$ /index.php?app=main&module=content
Which to my understanding looks for the url structure and allows 1 or 0 trailing "/"'s
But some parts of the website have a /urlPrefix/ to access, eg. mysite.com/membersArea/
and /membersArea/ will be apart of every query there. I'm having trouble accomodating for trailing ?s and &s in URLs like these.
RewriteRule ^secureUrl/\?(.*)$ /index.php?app=admin&$1
This is my attempt to handle everything from mysite.com/secureUrl/ to mysite.com/secureUrl/?var1=foo&var2=bar and after many server errors and a search, I find myself here.
This is the most complex line I have and between you and me, I couldn't tell you exactly what's happening other than it looks for /friendlyUrl/10DIGITKEY/(possible task)/?possiblevars=foo&var2=bar
RewriteRule ^friendlyUrl/([a-zA-Z0-9]{10})/?([a-z]*)/?\??(.*)$ /index.php?app=main&module=web&id=$1&$2&$3
Htaccess has always been my weakest subject, and as a webmaster I pay the price constantly, any help would be appreciated.
Need to input the same request to the PHP file (plus ANY query with or without ? or &) whether its just /friendlyUrl/ or /friendyUrl/?var=1, /friendlyUrl/&var=1, /friendlyUrl/var=1
You're looking to keep the query string of your request URI to remain as is, or to be included in the rewritten URL after the rewrite process is done.
For this purpose, you use the QSA flag in your RewriteRule directive. So, to rewrite /friendlyUrl/10DIGITKEY/(possible task)/?possiblevars=foo&var2=bar, you'd have:
RewriteRule ^friendlyUrl/([a-z\d]{10})/([^/]*)/?$ /index.php?app=main&module=web&id=$1&task=$2 [QSA]
Notice the QSA flag at the end. Also, keep in mind that I'm passing the second match (the possible task of your URL) as another variable (named task). This variable will be empty if nothing was found.
QSA|qsappend
When the replacement URI contains a query string, the default behavior
of RewriteRule is to discard the existing query string, and replace it
with the newly generated one. Using the [QSA] flag causes the
query strings to be combined.

Getting Query String item after htaccess redirect

I have a re-written URL that will look like one of the following:
http://www.example.com/anyfolder/some-more-here/
http://www.example.com/anyfolder/some-more-here/?ref=referralcode
Here is the rule I was playing with:
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/?$ /page.asp?folder=$1&url=$2&refcode=$3 [NC,L]
My question is that since (http://www.example.com/anyfolder/some-more-here/?ref=) is a rewritten URL that has an additional query string item on it after the rewrite is done, how do I get that "ref" item in my code? I can easily get "anyfolder" and "some-more-here" by doing a simple request, but I am confused on the "ref" value part. I think the QSA flag might be needed, just having trouble bringing it all together.
I am using Classic ASP on this site, but the solution probably applies to PHP as well.
Any help would be great. thank you in advance!
Dennis
ok as per your comment you do need QSA flag. Try your rule as:
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/?$ /page.asp?folder=$1&url=$2&refcode=$3 [NC,L,QSA]
This will preserve ?ref=... in the rewritten URI.
QSA (Query String Append) flag preserves existing query parameters while adding a new one.

Mod rewrite with query strings after .html

Im trying to perform a mod rewriting to my web site. And I used to do this rewriting for quite some time. Of course I am very much upto the task until I met new requirement to create a rewritten URL with query strings after .html
Example - http://kypseli/admin/catagory.html?parent_id=1
I have used this this regex.
RewriteRule ^([A-Za-z0-9-/_]+).([html])/?$ index.php?rt=$1&%{QUERY_STRING}
But the query string value (parent_id) not passing.
This is working with out the .html part for perfection.
http://kypseli/admin/catagory/?parent_id=1
But I want it with .html part, as I have found lots and lots of same technique usage every where in WWW. here is one example
http://www.espncricinfo.com/ci/content/video_audio/559158.html?genre=46
Can someone please help.
If you want to get all query string elements from the first query to be appended on your rewritten query you need to add the [QSA] flag to the rewriteRule, or [qsappend] if you prefer long and descriptive tags.
RewriteRule ^([A-Za-z0-9-/_]+)/?$ index.php?rt=$1 [qsappend]
Now I do not understand fully what you mean about '.html', but I hope this will be enough.
EDIT
your problem is maybe the dot, it should be escaped \. and it should be in the optionnal part if it is optionall so :
RewriteRule ^([A-Za-z0-9-/_]+)/?$ index.php?rt=$1 [qsappend]
RewriteRule ^([A-Za-z0-9-/_]+)\.html?$ index.php?rt=$1 [qsappend]
Or if you want only one rule, this should work (untested)
RewriteRule ^([A-Za-z0-9-/_]+)([\.html|/])?$ index.php?rt=$1 [qsappend]
And it's unsure you really need the ? in your rewriteRule, you may want to apply it even without query strings.

rewriting an extra query string with mod_rewrite

I hope to learn how to do regular expressions one day but for now I need a little help.
I have this set up in my .htaccess. So that a query to example.doc is internally forwarded to download.php?file=example.doc
RewriteRule ^(download.php.*) - [L]
RewriteRule ^(.+\.(doc|docx))$ download.php?file=$1
I need it changed slightly so that an extra query sting (always present) on the document is also internally rewritten and passed to the php. For example: a query to example.doc?verify=whatever would be internally rewritten to download.php?file=example.doc&verify=whatever
Thanks!
Neddy
The [QSA] flag should take care of this (Query String Append).

Get htaccess to add value as GET variable when redirecting

Simply put, what I want to achieve is this,
www.example.com/show-products/food should be redirected to
www.example.com/show_products.php?cat_id=1
or www.example.com/show-products/clothes should be redirected to
www.example.com/show_products.php?cat_id=8
But, I want htaccess to send the GET variables from the URL to the script as well.
So www.example.com/show-products/food?sort=price&sort_order=asc should be redirected to
www.example.com/show_products.php?cat_id=1?sort=price&sort_order=asc&cat_id=1
I know the way I formulated the question is not the best but hopefully you can make sense of it.
Thanks.
I found the answer, it's easier than I expected, the condition is:
RewriteRule ^food(/)?$ show_products.php?cat_id=1&%{QUERY_STRING}
The way that you've done it is fine, but keep in mind that you can also use the QSA flag. This automatically appends the existing query string to the query string which you generate with your rewrite, handling the concatenation for you automatically.
This would look like the following:
RewriteRule ^food(/)?$ show_products.php?cat_id=1 [QSA]

Resources