Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Something like <a href="https://stackoverflow.com/questions/117931/apache-modrewrite-one-rule-for-any-number-of-possibilities/119306#119306">this</a>?</p> <p>And see also <a href="https://stackoverflow.com/questions/1867373/recursive-mod-rewrite-for-search-engine-friendly-urls/1868818#1868818">this</a> for some additional info.</p> <p>The basic info is these rules</p> <pre><code>RewriteCond %{QUERY_STRING} ^(.*)$ RewriteRule ^(.*/)([^/]+)/([^/]+) $1?$2=$3&amp;%1 [L] RewriteCond %{QUERY_STRING} ^(.*)$ RewriteRule ^([^/]+)/ $1?%1 [L] </code></pre> <p>will rewrite the following</p> <pre><code>/mypage/param1/val1/param2/val2/param3/val3/... ---&gt; /mypage?param1=val1&amp;param2=val2&amp;param3=val3&amp;... </code></pre> <p>It takes the first parameter and calls that page. The rest of the parameters are turned into the query string.</p> <p>EDIT with more about how the code works.</p> <p>The first line (and the third) merely capture everything after the "?" so that it can be added back to the end of the rewritten URL. The same thing can be accomplished with the [QSA] flag, but I prefer to do it explicitly so it's visible.</p> <p>The string is saved in the %1 variable.</p> <p>The second line uses a regular expression to split the URL into three parts.</p> <p>$2 and $3 are the last two parts of the path. $1 is everything before that.</p> <p>So this:</p> <pre><code>/mypage/param1/val1/param2/val2/param3/val3/param4/val4/param5/val5/ </code></pre> <p>Becomes this:</p> <pre><code>$1 $2 $3 /mypage/param1/val1/param2/val2/param3/val3/param4/val4/ param5 val5 </code></pre> <p>and is rewritten as this (spaces added for readability):</p> <pre><code>/mypage/param1/val1/param2/val2/param3/val3/param4/val4/ ? param5 = val5 </code></pre> <p>If your URL uses separators other than slashes, you can modify the regular expression accordingly.</p> <p>The [L] ("last") flag prevents the rest of the rules from running. HOWEVER -- and this is the key -- mod_rewrite calls the webserver with the new URL and the new URL is sent through mod_rewrite again! This is sometimes called mod_rewrite recursion and is exactly what you want for this to work.</p> <p>The next time through, the next two parts are stripped off the end and added to the query string.</p> <p>This:</p> <pre><code>$1 $2 $3 %1 /mypage/param1/val1/param2/val2/param3/val3 param4 val4 ? param5=val5 </code></pre> <p>Becomes this:</p> <pre><code>/mypage/param1/val1/param2/val2/param3/val3 ? param4 = val4 &amp; param5=val5 </code></pre> <p>This keeps on happening until all the parts of the URL are converted into parts of the query string. When that happens, the second line doesn't match but the fourth line does. That is what gives you your final URL to call the program you want. The links above show answers to slightly different variations that allow for different rules for that.</p> <p>Also note that there are limits to the number of times mod_rewrite can recurse. See the settings for <code>MaxRedirects</code> in .htaccess and <code>LimitInternalRecursion</code> in your apache conf file.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload