Note that there are some explanatory texts on larger screens.

plurals
  1. PO(Apache Mod_Rewrite) Please help to condense my query
    text
    copied!<p>I am trying to write a set of mod_rewrite rules to fulfill the following conditions:</p> <pre><code>1) /aaa (redirects to) --&gt; /xxx_aaa.php 2) /aaa/ --&gt; /xxx_aaa.php 3) /aaa?w=x&amp;y=z --&gt; /xxx_aaa.php?w=x&amp;y=z 3) /aaa/?y=z --&gt; /xxx_aaa.php?y=z 4) /aaa/bbb/ --&gt; /xxx_aaa_bbb.php 5) /aaa/bbb/ccc/ --&gt; /xxx_aaa_bbb_ccc.php 6) /aaa/bbb/cc12-34cc --&gt; /xxx_aaa_bbb_cc12-34cc.php 7) /aaa/bbb/ccc/?y=z --&gt; /xxx_aaa_bbb_ccc?y=z.php </code></pre> <p>The above list isn't exhaustive, but it gives you an idea of all the possible permutations I am aiming to cover. Namely, these are:</p> <p>1) Up to three subdirectory levels<br> 2) Works whether closing forward slash is present or not<br> 3) Pass GET query strings, if present</p> <p>This is that I use at the moment:</p> <pre><code>Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteRule ^([0-9a-zA-Z\-]+)$ /xxx_$1.php [L] RewriteRule ^([0-9a-zA-Z\-]+)/$ /xxx_$1.php [L] RewriteRule ^([0-9a-zA-Z\-]+)/\?([0-9a-zA-Z\-=&amp;]+)$ /xxx_$1.php?$2 [L] RewriteRule ^([0-9a-zA-Z\-]+)/([0-9a-zA-Z\-]+)$ /xxx_$1_$2.php [L] RewriteRule ^([0-9a-zA-Z\-]+)/([0-9a-zA-Z\-]+)/$ /xxx_$1_$2.php [L] RewriteRule ^([0-9a-zA-Z\-]+)/([0-9a-zA-Z\-]+)/\?([0-9a-zA-Z\-=&amp;]+)$ /xxx_$1_$2.php?$3 [L] RewriteRule ^([0-9a-zA-Z\-]+)/([0-9a-zA-Z\-]+)/([0-9a-zA-Z\-]+)$ /xxx_$1_$2_$3.php [L] RewriteRule ^([0-9a-zA-Z\-]+)/([0-9a-zA-Z\-]+)/([0-9a-zA-Z\-]+)/$ /xxx_$1_$2_$3.php [L] RewriteRule ^([0-9a-zA-Z\-]+)/([0-9a-zA-Z\-]+)/([0-9a-zA-Z\-]+)/\?([0-9a-zA-Z\-=&amp;]+)$ /xxx_$1_$2_$3.php?$4 [L] </code></pre> <p>And it seems to work just fine.</p> <p>But I can't help but think that there must be a much more elegant way of doing it - perhaps even condensing all the above into one single line.</p> <p>Any ideas? Any help much appreciated, thanks. :)</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