pavlenko
pavlenko

Reputation: 665

Using if statement in .htaccess file

using basic regex code in .htaccess file, to remove .php extension from site url, like this:

# Externally redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

##Internally redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

This works fine,but I also have mail.php file, for sending Emails, and can't post data to this file,because of rules from .htaccess, that path is like this: www.site/mail instead www.site/mail.php.

So, is there way to set in .htaccess something like:

If url is: www.site/mail.php don't use rule for cutting extension?

Upvotes: 1

Views: 105

Answers (1)

anubhava
anubhava

Reputation: 784918

Actually your first rule shouldn't remove .php for any POST request otherwise you will loose all POST data. You can fix it using:

# Externally redirect if not POST request
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L,NE]

##Internally redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Upvotes: 1

Related Questions