How to pass parameters to Rewrite Rules with a .php file name?

I have a website that successfully transfers content after the first slash to the specified .php file. (elegant: remove ".php")

site.com/azamat to site.com/azamat.php

RewriteCond %{REQUEST_FILENAME} !-d    
RewriteCond %{REQUEST_FILENAME}\.php -f   
RewriteRule ^(.*)$ $1.php [NC]

How to add additional parameters to these rules, so I can pass page parameters, for example:

site.com/azamat/bagatov to site.com/azamat.php?page=bagatov

or

site.com/thisisthefile/andparams to site.com/thisisthefile.php?page=andparams

+5
source share
1 answer

Try adding these rules (keep the ones you already have):

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)/.+$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([^/]+)/(.+)$ /$1.php?page=$2 [L]

This is more or less similar to what you have, with the exception of using %1backreference to check if the first part of the URI exists as a php file. This is a match for the first part:

RewriteCond %{REQUEST_URI} ^/([^/]+)/.+$

And this is fheck to see if grouping exists ([^/]+)as php file:

RewriteCond %{DOCUMENT_ROOT}/%1.php -f

, /, , page.

+2

All Articles