<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: Regex woes.</title>
	<link>http://ask.metafilter.com/93745/Regex-woes/</link>
	<description>Comments on Ask MetaFilter post Regex woes.</description>
	<pubDate>Tue, 10 Jun 2008 18:10:30 -0800</pubDate>
	<lastBuildDate>Tue, 10 Jun 2008 18:10:30 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: Regex woes.</title>
		<link>http://ask.metafilter.com/93745/Regex-woes</link>	
		<description>Not quite getting how mod_rewrite regex works when flatting urls with multiple variables. &lt;br /&gt;&lt;br /&gt; Hello hello,&lt;br&gt;
&lt;br&gt;
Ok, so I&apos;m working on another website but I have run into what I&apos;m sure is a pretty basic problem that I can&apos;t seem to wrap my head around. I use mod_rewrite pretty frequently to flatten the most basic types of dynamic urls, those with only one variable. But now I need to figure out how to configure my htaccess to handle urls that always have one variable, but sometimes also have 2-3.&lt;br&gt;
&lt;br&gt;
Now if I knew the same number of variables would be present all the time I think I could handle it, but when there is a variable number of variables I just can&apos;t seem to figure out what I&apos;m doing.&lt;br&gt;
&lt;br&gt;
Here is an example of what I do know how to do. Let&apos;s say I want to change the url: &lt;br&gt;
&lt;br&gt;
   http://mywebsite.com/profile/jeremy/&lt;br&gt;
&lt;br&gt;
into:&lt;br&gt;
&lt;br&gt;
   http://mywebsite.com/profile.php?name=jeremy&lt;br&gt;
&lt;br&gt;
I&apos;d use:&lt;br&gt;
&lt;br&gt;
   ReWriteRule ^profile/([A-Za-z]+)/$ /profile.php?name=$1&lt;br&gt;
&lt;br&gt;
but if sometimes I also add extra variables like so:&lt;br&gt;
&lt;br&gt;
   http://mywebsite.com/profile/jeremy/action/sort/order/desc/&lt;br&gt;
&lt;br&gt;
into:&lt;br&gt;
&lt;br&gt;
   http://mywebsite.com/profile.php?name=jeremy&amp;amp;action=sort&amp;amp;order=desc&lt;br&gt;
&lt;br&gt;
Then I just can&apos;t seem to wrap my head around it. Especially if depending on circumstances I might have urls like so where the 2nd variable in the previous example is missing, but the third is still round:&lt;br&gt;
&lt;br&gt;
   http://mywebsite.com/profile/jeremy/order/desc/&lt;br&gt;
  &lt;br&gt;
I&apos;ve Googled around but it seems most websites toughing on the subject are either too simple (and just give examples with single variables) or are too complicated and assume I already have abase level of regex knowledge which I sadly lack. &lt;br&gt;
&lt;br&gt;
So would any kindly Mefite want to give me a walk through on what exactly I should be trying to do (and most importantly why, so that I can avoid  just rote copy/pasting and instead be able to solve these kind of problems myself in the future =)&lt;br&gt;
&lt;br&gt;
Thanks much!&lt;br&gt;
Jeremy</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2008:site.93745</guid>
		<pubDate>Tue, 10 Jun 2008 17:27:29 -0800</pubDate>
		<dc:creator>Jezztek</dc:creator>
		
			<category>regex</category>
		
			<category>htaccess</category>
		
			<category>url</category>
		
	</item> <item>
		<title>By: fishfucker</title>
		<link>http://ask.metafilter.com/93745/Regex-woes#1371166</link>	
		<description>Consider using the [QSA] flag, which appends existing get queries to the rewrite to take care of some of the query strings. (&lt;a href=&quot;http://rewrite.drbacchus.com/rewritewiki/Flags_2fQSA&quot;&gt;See here for a more elaborate explanation&lt;/a&gt;). I do not feel like ordering/sorting info needs to be in a URL -- that&apos;s fine for a query string. As for actions, simply have two rules (I believe you need the [L] flag for this), one which rewrites strings with actions, one which does not:&lt;br&gt;
&lt;pre&gt;&lt;br&gt;
ReWriteRule ^profile/([A-Za-z]+)/([A-Za-z]+)/?$ /profile.php?name=$1&amp;amp;action=$2 [QSA,L]&lt;br&gt;
ReWriteRule ^profile/([A-Za-z]+)/?$ /profile.php?name=$1 [QSA,L]&lt;br&gt;
&lt;/pre&gt;&lt;br&gt;
&lt;br&gt;
I&apos;m sure you could do it in one, but this seems much more clear to me. Readability and maintainability is typically more important than (presumed) optimization. If you&apos;re doing any more rewriting than one or two vars, consider tossing the entire string into a get var that is processed by a server-side script. &lt;br&gt;
&lt;br&gt;
It also looks like you&apos;re trying to set both query KEYS and VALUES in your urls -- ie, you want to do something like : &lt;br&gt;
&lt;pre&gt;&lt;br&gt;
ReWriteRule ^profile/([A-Za-z]+)/([A-Za-z]+)/([A-Za-z]+)?$ /profile.php?name=$1&amp;amp;$2=$3 [QSA,L]&lt;br&gt;
&lt;/pre&gt;&lt;br&gt;
&lt;br&gt;
In theory, you can do this, but I think it&apos;s going to be super confusing, and I&apos;ve personally never seen an app work this way -- it forces you to have to handle a crapload of 404s that would otherwise be routed by apache. &lt;br&gt;
 &lt;br&gt;
Something else to keep in mind: are you limiting user names to just letters? Your rewrite will fail for people using underscores, spaces, or numbers in their names.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.93745-1371166</guid>
		<pubDate>Tue, 10 Jun 2008 18:10:30 -0800</pubDate>
		<dc:creator>fishfucker</dc:creator>
	</item><item>
		<title>By: fishfucker</title>
		<link>http://ask.metafilter.com/93745/Regex-woes#1371170</link>	
		<description>Er, to better explain my first example, your URL might look like : &lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;br&gt;
/profile/jeremy/sort?order=DESC &lt;br&gt;
&lt;/pre&gt;&lt;br&gt;
&lt;br&gt;
i feel like if you&apos;re sending an &apos;order&apos; get var, it&apos;s pretty obvious you&apos;re wanting to sort, so really, the sort action is redundant, but maybe you&apos;ll have a diff action, like &lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;br&gt;
/profile/jeremy/blogs/?sort=date&amp;amp;order=DESC&lt;br&gt;
&lt;/pre&gt;&lt;br&gt;
&lt;br&gt;
Note how I moved the sort field and order into the get vars -- I really think that&apos;s the most appropriate place for it, since you&apos;re *modding* a default. IMO, URLs should always go to a DEFAULT VIEW, while a MOD of that DEFAULT VIEW should appear as a GET var. It just seems more consistent. That way I know I can get to a default view by removing all get vars.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.93745-1371170</guid>
		<pubDate>Tue, 10 Jun 2008 18:14:01 -0800</pubDate>
		<dc:creator>fishfucker</dc:creator>
	</item><item>
		<title>By: pocams</title>
		<link>http://ask.metafilter.com/93745/Regex-woes#1371188</link>	
		<description>Seconding &lt;b&gt;fishfucker&lt;/b&gt; heartily - the reason you&apos;re having trouble doing this is that it&apos;s really a weird thing to do.  Things like sort and order really do belong in the query string, even if you&apos;re otherwise beautifying your URLs.&lt;br&gt;
&lt;br&gt;
Now, that said, the following rules do what you want, modulo the ^/profile/ part which you may have to change:&lt;br&gt;
&lt;code&gt;&lt;br&gt;
        RewriteRule ^/profile/([^/]+)/([^/]+)/([^/]+)/?(.*) /profile/$1/$4?$2=$3 [N,QSA]&lt;br&gt;
        RewriteRule ^/profile/([^/]+)/$ /profile.php?name=$1 [QSA]&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
The first rule matches /profile/ then at least three slash-separated chunks of characters (I didn&apos;t preserve your letters-only semantics here.)  It then redirects to /profile/ again, preserving the first chunk (which is the name in your example) and extracting the next two, turning them into a piece of the query string ($2=$3).  $4 is a general &quot;rest of the string&quot;, since we need to preserve pieces of the path we haven&apos;t turned into query string chunks yet.  The N flag means to run the ruleset again with the changed URL, and QSA means &quot;keep the old query string and just add on what I&apos;ve added here&quot;.  The N flag provides the magic here, since we run this rule over and over again until we run out of path chunks to convert.&lt;br&gt;
&lt;br&gt;
The second rule matches when we&apos;ve run out of slash-separated chunks - we need a special case because you&apos;re using /profile/jeremy/, not /profile/name/jeremy/.  Again, we preserve the old query string and simply add the name=$1 to capture the name, which you&apos;re familiar with.&lt;br&gt;
&lt;br&gt;
The rules above aren&apos;t polished; for instance, they fail if you access a URL like /profile/jeremy/sort/desc/extra/.  That&apos;s okay, because honestly I don&apos;t think you&apos;re going to want to go through with this.  It&apos;s a testament to mod_rewrite&apos;s flexibility that you can do it pretty easily, but you really, really don&apos;t want to go down that road.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.93745-1371188</guid>
		<pubDate>Tue, 10 Jun 2008 18:28:38 -0800</pubDate>
		<dc:creator>pocams</dc:creator>
	</item><item>
		<title>By: fishfucker</title>
		<link>http://ask.metafilter.com/93745/Regex-woes#1371197</link>	
		<description>&lt;small&gt;wow, pocams, i am in awe. This is the first time I&apos;ve ever seen a recursive mod_rewrite rule.&lt;/small&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.93745-1371197</guid>
		<pubDate>Tue, 10 Jun 2008 18:41:37 -0800</pubDate>
		<dc:creator>fishfucker</dc:creator>
	</item><item>
		<title>By: Jezztek</title>
		<link>http://ask.metafilter.com/93745/Regex-woes#1371258</link>	
		<description>Wow, thanks much guys, I wasn&apos;t even aware of the QSA flag. Which I suppose is why I couldn&apos;t find good info on doing what I was trying to do =)</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.93745-1371258</guid>
		<pubDate>Tue, 10 Jun 2008 19:27:18 -0800</pubDate>
		<dc:creator>Jezztek</dc:creator>
	</item>
	</channel>
</rss>
