<?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>Route of Queue &#187; query</title>
	<atom:link href="http://www.routeofqueue.com/tag/query/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.routeofqueue.com</link>
	<description>I have issues</description>
	<lastBuildDate>Sun, 30 May 2010 11:18:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Mod rewrite URL with 2 question marks?</title>
		<link>http://www.routeofqueue.com/2009/02/mod-rewrite-url-with-2-question-marks/</link>
		<comments>http://www.routeofqueue.com/2009/02/mod-rewrite-url-with-2-question-marks/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 21:46:42 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Hints and Tips]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[colleague]]></category>
		<category><![CDATA[condition]]></category>
		<category><![CDATA[fact]]></category>
		<category><![CDATA[file folder]]></category>
		<category><![CDATA[job]]></category>
		<category><![CDATA[legit]]></category>
		<category><![CDATA[Mod]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[query string]]></category>
		<category><![CDATA[querystring]]></category>
		<category><![CDATA[question]]></category>
		<category><![CDATA[question mark]]></category>
		<category><![CDATA[question marks]]></category>
		<category><![CDATA[request]]></category>
		<category><![CDATA[request url]]></category>
		<category><![CDATA[rewrite rule]]></category>
		<category><![CDATA[RewriteCond]]></category>
		<category><![CDATA[RewriteRule]]></category>
		<category><![CDATA[RewriteRules]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[simple solution]]></category>
		<category><![CDATA[URL]]></category>
		<category><![CDATA[workaround]]></category>
		<category><![CDATA[www]]></category>

		<guid isPermaLink="false">http://www.routeofqueue.com/?p=14</guid>
		<description><![CDATA[A colleague asked me for some help with a few RewriteRules in his .htaccess today. One of the links he was trying to rewrite was unusual in the fact that is had 2 question marks &#8220;?&#8221; in it. Let&#8217;s say the url looked like: www.site.com/2009/1/is-this-a-legit-url?-i-don't-know? and you wanted to rewrite that to: www.site.com/2009/1/is-this-a-legit-url-i-dont-know/ You might [...]]]></description>
			<content:encoded><![CDATA[<p>A colleague asked me for some help with a few RewriteRules in his .htaccess today. One of the links he was trying to rewrite was unusual in the fact that is had 2 question marks &#8220;?&#8221; in it.</p>
<p>Let&#8217;s say the url looked like:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">www.site.com/<span style="color: #ff0000;">2009</span>/<span style="color: #ff0000;">1</span>/is-this-a-legit-url?-i-don<span style="color: #7f007f;">'t-know?</span></pre></div></div>

<p>and you wanted to rewrite that to:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">www.site.com/<span style="color: #ff0000;">2009</span>/<span style="color: #ff0000;">1</span>/is-this-a-legit-url-i-dont-know/</pre></div></div>

<p>You might think the rewrite would be simple, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">RewriteRule</span> ^<span style="color: #ff0000;">2009</span>/<span style="color: #ff0000;">1</span>/is-this-a-legit-url\?-don%27t-know\?/?$ /<span style="color: #ff0000;">2009</span>/<span style="color: #ff0000;">1</span>/is-this-a-legit-url-i-dont-know/? [R=<span style="color: #ff0000;">301</span>,NC,L]</pre></div></div>

<p>If you run that though, apache will throw a wobbly and tell you that &#8220;The requested URL /2009/1/is-this-a-legit-url was not found on this server.&#8221;</p>
<p>The reason for this is the server thinks everything after the first &#8220;?&#8221; is the querystring, so in this case, the apache is looking for a file/folder called &#8220;/2009/1/is-this-a-legit-url&#8221; and it will pass the querystring through as &#8220;-i-don&#8217;t-know?&#8221;</p>
<p>Of course, the simple solution would be to just match up to the first question mark in the rule, but that might not always be appropriate &#8211; e.g. if the string began with a question mark for whatever reason!</p>
<p>We came up with a cunning workaround for this by matching both the request url and the querystring like so:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">RewriteCond</span> %{QUERY_STRING} ^-i-don%27t-know\?/?$ [NC]
<span style="color: #00007f;">RewriteRule</span> ^<span style="color: #ff0000;">2009</span>/<span style="color: #ff0000;">1</span>/is-this-a-legit-url$ /<span style="color: #ff0000;">2009</span>/<span style="color: #ff0000;">1</span>/is-this-a-legit-url-i-dont-know/? [R=<span style="color: #ff0000;">301</span>,NC,L]</pre></div></div>

<p>Now, if a request like the original one above comes in, the query string will match the rewrite condition, and the requested URL will match the rewrite rule! :)</p>
<p>Note the extra question mark at the end of the rewritten rule &#8220;/is-this-a-legit-url-i-dont-know/? [R=301,NC,L]&#8221; &#8211; you need that in there to stop the server trying to push the original query string through.</p>
<p>Without it, your URL will be redirected to:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">www.site.com/<span style="color: #ff0000;">2009</span>/<span style="color: #ff0000;">1</span>/is-this-a-legit-url-i-dont-know/?-i-don%2527t-know%3f</pre></div></div>

<p>Unfortunately, using this method does mean that if you have any variable in the query string that you want to push through, you won&#8217;t be able to&#8230; Chances are though, that if you have 2 question marks, something&#8217;s gone wrong anyway, so this fix is better than nothing! ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.routeofqueue.com/2009/02/mod-rewrite-url-with-2-question-marks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
