Mod_rewrite to remove .php extension and save GET parameters

I reviewed these solutions, and none of them work properly on my end:

mod_rewrite to remove .php but still serve the .php file?

How to remove the file extension from the site address?

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

and they just don't do the following what I need to do:

1) remove the .php extension from the files and instead of /index.php display / index

2) save the GET parameters (which I read and save in the session cookies when loading the file in the header), so instead of /index.php?a=1&b=2 it is possible to display / index / a 1 / b2

3) work on subdomains and https: // without completely filling out the URL or ending in an infinite loop or something like that.

Does anyone know how to put together these rules so that they correctly cover 3 points?

This is what I work with as a starting point:

RewriteCond %{THE_REQUEST} (\.php(.*)\sHTTP/1)
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]
+5
source share
3 answers

You see it wrong. You do not want to redirect the request to the .php file to a clean address, because you never want the user to see the .php in the first place. Instead, you need to take a request that does not end with .php, and then check if the file (under the surface) really has the .php extension. Then, if so, calmly rewrite the request to add the .php extension so that the user never sees the extension.

Try the following:

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /$1.php [QSA]

RewriteCond , ( DOCUMENT_ROOT) ".php" . , RewriteRule , ".php" . QSA mod_rewrite , GET .

, DOCUMENT_ROOT , $1 RewriteCond.

, mod_rewrite , , mod_rewrite .

+6

mod_rewrite .htaccess httpd.conf, .htaccess DOCUMENT_ROOT:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

, , R=302 R=301. ( R=301 () mod_rewrite).

+3

:

RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]
0

All Articles