htaccess redirect with parameter and excluding one folder

Viewed 84

I've been trying out a few permutations and still can't figure this out. I have 3 requirements:

  • forward everything from xxx.php to just xxx
  • mydomain/sentence actually loads mydomain/page?q=sentence
  • exclude a folder e.g. mydomain/logs

So I managed to get the first two to work using:

Options +FollowSymLinks
RewriteEngine On
#RewriteCond %{REQUEST_URI}!^/panel/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ page.php?q=$1 [NC,L]
# RewriteRule ^([^\.]+)$ $1.php [NC,L]

If I uncomment the third line everything breaks. Any help is appreciated! Also please point out any errors I might have. Thanks in advance! I apologize for the bad formatting. The editor kept giving errors no matter how I typed.

1 Answers

You can try the following rule to only redirect to PHP files that actually exist. I also believe that this rule has to come before the other rewrite rule because the condition of the other rule would also match.

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

#RewriteCond %{REQUEST_URI}!^/panel/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ page.php?q=$1 [NC,L]
Related