简体   繁体   中英

How to load php file without php extension

I have this working htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/([0-9]+)/?$ test.php?id=$1 [L]

my question for this if I have that rule on my htaccess how can I load php file that have no .php extension.

I add this to my htacess

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

It works fine http://localhost/myprojectfolder/account but this will not work anymore http://localhost/myprojectfolder/1 how can I make them both working ?

Thank you in advance.

Try adding another condition to your php extension rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

that ensures that when you add the .php extension, it actually points to a file that exists.

If you are using this URL http://localhost/myprojectfolder/1 as in your other question. Then test shouldn't even be in the first rule. Then you can just make sure the file exists.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ test.php?id=$1 [L]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM