Search code examples
apache.htaccessmod-rewriteurl-rewriting

Merging new `mod_rewrite` rules and preserving old behaviour, what is wrong?


I am using Apache2 with the following (good) .htaccess at the root of my site,

 #OLD (GOOD, IS WORKING!)
 # If requested url is an existing file or folder, don't touch it
 RewriteCond %{REQUEST_FILENAME} -d [OR]
 RewriteCond %{REQUEST_FILENAME} -f
 RewriteRule . - [L]

 RewriteRule ^(urn|URN):(.+)$     index.php?urn=$2 [L]
 RewriteRule ^([a-zA-Z\d\-]+)$    index.php?obj=$1 [L]

Now I need to merge a new condition for the folder xWiki

#NEW: some conflicts, how to preserve old and add /xWiki* behaviour?
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^xWiki/?(.*)$ /xWiki/index.php?title=$1 [L,QSA]

How to "merge" preserving old behaviour except to /xWiki?

Only adding the RewriteRule ^xWiki/?(.*)$ not works, and the !-f is in conflict with the same -f cond...


Solution

  • Have it this way:

     #OLD (GOOD, IS WORKING!)
     # If requested url is an existing file or folder, don't touch it
     RewriteCond %{REQUEST_FILENAME} -d [OR]
     RewriteCond %{REQUEST_FILENAME} -f
     RewriteRule . - [L]
    
     RewriteRule ^xWiki/(.*)$ /xWiki/index.php?title=$1 [L,QSA]
    
     RewriteRule ^(urn|URN):(.+)$     index.php?urn=$2 [L,QSA]
     RewriteRule ^([a-zA-Z\d\-]+)$    index.php?obj=$1 [L,QSA]
    
    • the main issue is to place the RewriteRule in the correct place (before other RewriteRules), and undertand that there are no conflict with -f and !-f (no changes in the -f is the merge solution).
    • a good optimization was to change regex ^xWiki/?(.*)$ to ^xWiki/(.*)$.