<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Simone Carletti&#039;s Blog &#187; querystring</title>
	<atom:link href="http://www.simonecarletti.com/blog/tags/querystring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.simonecarletti.com/blog</link>
	<description>Simone Carletti&#039;s personal ramblings on programming, syndication, search engines &#38; marketing.</description>
	<lastBuildDate>Tue, 07 Feb 2012 08:48:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Apache RewriteRule and query string</title>
		<link>http://www.simonecarletti.com/blog/2009/01/apache-rewriterule-and-query-string/</link>
		<comments>http://www.simonecarletti.com/blog/2009/01/apache-rewriterule-and-query-string/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 08:05:16 +0000</pubDate>
		<dc:creator>Simone Carletti</dc:creator>
				<category><![CDATA[Server / Apache]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[querystring]]></category>

		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=135</guid>
		<description><![CDATA[A quick reference for dealing with query string in Apache rewrite rules.]]></description>
			<content:encoded><![CDATA[<p>At first look, the way Apache <code>mod_rewrite</code> module deals with query strings can be a little confusing. From <a href="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond">the RewriteRule documentation</a>:</p>
<blockquote><p><strong>qsappend|QSA&#8217; (query string append)</strong><br />
This flag forces the rewrite engine to append a query string part of the substitution string to the existing string, instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule.</p></blockquote>
<p>Let&#8217;s try to <a href="http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/">redirect a page depending on its query string</a>. Our rewrite rule should looks like this:</p>
<div class="codecolorer-container text default brush: apache;" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">RewriteCond %{QUERY_STRING} ^id=([0-9]*)$<br />
RewriteRule ^page\.php$ http://mydomain.site/page/%1.php [R=302,L]</div></td></tr></tbody></table></div>
<p><span id="more-135"></span>According to the official documentation, you would expect the following behavior:</p>
<div class="codecolorer-container text default brush: text;" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">/page.php?id=37 -&gt; http://mydomain.site/page/37.php<br />
/page.php?id=40 -&gt; http://mydomain.site/page/40.php<br />
# and so on</div></td></tr></tbody></table></div>
<p>However, if you don&#8217;t append something new, then <strong>the original query is passed through the rules unchanged</strong> by default.</p>
<div class="codecolorer-container text default brush: text;" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">/page.php?id=37 -&gt; http://mydomain.site/page/37.php?id=37<br />
/page.php?id=40 -&gt; http://mydomain.site/page/40.php?id=40<br />
# and so on</div></td></tr></tbody></table></div>
<p>If you want to discard the original query string you must append an empty question mark at the end of the rule. Let&#8217;s call it the <strong>query string not append</strong> or <strong>query string discard</strong> flag.</p>
<div class="codecolorer-container text default brush: apache;" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">RewriteCond %{QUERY_STRING} ^id=([0-9]*)$<br />
RewriteRule ^page\.php$ http://mydomain.site/page/%1.php&lt;strong&gt;?&lt;/strong&gt; [R=302,L]</div></td></tr></tbody></table></div>
<p>Here&#8217;s a quick reference for dealing with query string in a RewriteRule.</p>
<div class="codecolorer-container text default brush: text;" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Keep original query (default behavior)<br />
RewriteRule ^page\.php$ /target.php [L]<br />
# from http://example.com/page.php?foo=bar<br />
# to &nbsp; http://example.com/target.php?foo=bar<br />
<br />
Discard original query<br />
RewriteRule ^page\.php$ /target.php? [L]<br />
# from http://example.com/page.php?foo=bar<br />
# to &nbsp; http://example.com/target.php<br />
<br />
Replace original query<br />
RewriteRule ^page\.php$ /target.php?bar=baz [L]<br />
# from http://example.com/page.php?foo=bar<br />
# to &nbsp; http://example.com/target.php?bar=baz<br />
<br />
Append new query to original query<br />
RewriteRule ^page\.php$ /target.php?bar=baz [QSA,L]<br />
# from http://example.com/page.php?foo=bar<br />
# to &nbsp; http://example.com/target.php?foo=bar&amp;bar=baz</div></td></tr></tbody></table></div>
<p>Related posts<ol>
<li><a href='http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/' rel='bookmark' title='Apache .htaccess query string redirects'>Apache .htaccess query string redirects</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/01/logging-external-referers-with-apache/' rel='bookmark' title='Logging external referers with Apache'>Logging external referers with Apache</a></li>
<li><a href='http://www.simonecarletti.com/blog/2009/12/generic-apache-configuration-snippets/' rel='bookmark' title='Generic Apache configuration snippets'>Generic Apache configuration snippets</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.simonecarletti.com/blog/2009/01/apache-rewriterule-and-query-string/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
	</channel>
</rss>

