How to use Apache Mod_rewrite to remove php extension while keeping GET parameters?

I tried to remove PHP extensions from my site. When the user requests a PHP file, PHP will be deleted and the user will be redirected, and when the user will enter the URL without PHP, the actual PHP file will be served. This works well unless the URL has a GET parameter. My rules are as follows:

# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php\sHTTP/1)
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]

# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# rewrite to FILENAME.php if such file does exist and is not a folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]

I thought it should already be possible to remove php, even if there is any GET parameter, but it failed. I also tried something like this:

RewriteCond %{THE_REQUEST} (\.php\sHTTP/1)
RewriteRule ^(.)\.php(.)$ $1$2 [R=301,L,QSA]

It also did not work, php still exists. But if I try:

RewriteRule ^(.)\.php(.)$ $1$2 [R=301,L,QSA]

those. RewriteCond removal, php extension is removed and the settings are saved, but the page will not be displayed as the browser says there were too many redirects.

, ,

+5
2

SteAp . , .

# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php\sHTTP/1)
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]

, PHP. RewriteCond , - .. - ( php, php, ...)

, http://domain.com/file.php?.... HTTP/1.1 - , RewriteCond , .

, :

# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php(.*)\sHTTP/1)
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]

, , .

, -, ( , ? lol)

+2

, %{QUERY_STRING}, .

.

0

All Articles