I have a web application with multiple pages. The .htaccess file redirects everything after the domain-name.com/ to my index.php file, which processes the input and renders the appropriate page.

However, I also have some actual directories under domain-name.com/ which need to be excepted from the redirect. For example, my PHPMyAdmin directory (/pma).

Everything was working fine until I added the line to redirect reports/[name] to the index page also. Now, my /pma doesn't go to the PMA directory and instead goes to my app's index.php.

If I use /pma/index.php, it works, but it doesn't make sense why /pma would stop working with that one new rewrite rule.

Commenting out the RewriteRule for reports causes everything to work again.

I can't figure out why. Thanks for any help!

.htaccess file:

RewriteEngine On

# Redirect http to https (http://htaccesscheatsheet.com/#force-https)
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

# Ignore the following directories (don't remap to page engine)
RewriteCond %{REQUEST_URI} !^\/(pma*|css)

#Redirect all /reports/[report name] requests to index.php?page=reports/[report name]
RewriteRule ^reports/([a-z0-9\-_]+)\/{0,1}$ index.php?page=reports/$1   [NC,QSA,L]

#Redirect all /[pagename] requests to index.php?page=[pagename]&[querystring]
RewriteRule ^([a-z0-9\-_]+)\/{0,1}$ index.php?page=$1   [NC,QSA,L]

# Return .json files with the correct mime type (needed to support manifest.json)
AddType application/json .json
  • 1
    /pma/index.php would "work" because it doesn't match the RewriteRule pattern. You don't need to escape slashes in the mod_rewrite regex (but you've not escaped them all). \/{0,1} is the same as /?. – MrWhite Aug 25 '17 at 10:17

Exceptions like this are easier to apply as final rules that don't do any rewriting. Replace your RewriteCond line with:

RewriteRule ^pma/ - [L]
RewriteRule ^css/ - [L]

RewriteCond only applies to the next RewriteRule. You now have two rules, each of which need the exception.

So just repeat that RewriteCond line like this:

#Redirect all /[pagename] requests (with exceptions) to index.php?page=[pagename]&[querystring]
RewriteCond %{REQUEST_URI} !^\/(pma*|css)
RewriteRule ^([a-z0-9\-_]+)\/{0,1}$ index.php?page=$1   [NC,QSA,L]

An alternative way to do it, if you like PCRE regular expressions is something like:

RewriteRule ^(?!pma|css)([a-z0-9\-_]+)\/{0,1}$ index.php?page=$1   [NC,QSA,L]

However, that's a bit obscure. If you're going to have several rules with exceptions, then I'd accept David's solution is easier to read.

  • !^\/(pma*|css) - that asterisk looks erroneous (but harmless as it happens). – MrWhite Aug 25 '17 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.