1

I have the following rules in my htaccess file to remove .php extension and do a 301 redirect to the extensionless URL:

# To remove .php extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]

# To check whether the file exists then set it back internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.*$ $0.php [L]

I want to apply the following rules on a PHP file:

RewriteRule ^test/([0-9]+)$ test.php?id=$1 [L]

The above rule leads to 500 internal server error. If I remove the first set of rules, the second rule works again. So, there is some conflict with both set of rules.

  • 1
    This looks like it should work OK. The natural position for that directive is between the two preceding rule blocks (after the external redirect), however, the order isn't so critical in this instance. Make sure you've cleared the browser cache (since any 301s will have been cached by the browser, which could cause problems). What URL are your requesting? Do you have any other directives? (Any other .htaccess files?) You should also check your error.log for the details of this error. – MrWhite Nov 5 '17 at 11:55
1

The problem is the order of the rules :)

When I changed the order like the following, everything worked:

RewriteRule ^test/([0-9]+)$ test.php?id=$1 [L]

# To remove .php extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]

# To check whether the file exists then set it back internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.*$ $0.php [L]=

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.