403 forbidden after rewriting my url to remove file extensions

Error Message

infinityfree errors 403

Other Information

When I add this htaccess file to my htdocs, it perfectly rewrites the url to remove the filenames from my addressbar. but whenver I load a webpage that has additional addressbar info like

webpage.com/Contact?event=packagedeal Infinityfree returns a 403 forbidden instead of loading the webpage.com/contact.php?event=packagedeal and rewriting it to remove the .php

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+(.+)\.php(\?[^\s]*)?\s [NC]

RewriteRule ^ /%1%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^.]+)$ $1.php [NC,L,QSA]

changing the href to exclude .php seems to fix this issue.

Is this the only fix? Id like to keep my page functioning without htaccess

You can definitely do what you want with .htaccess rules! The difficult part is figuring out the right rules, because .htaccess rules can be complicated. I too often have issues with figuring out the right rules.

In this case, the fix is pretty simple.

Instead of this line:

RewriteRule ^([^.]+)$ $1.php [NC,L,QSA]

You can just do this instead:

RewriteRule ^([^.]+) $1.php [NC,L,QSA]

I think the dollar sign at the end is preventing the rule from being applied when a query string is present. I think this should solve it, but I haven’t tested it, so I’m not sure.

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.