htaccess redirect any urls ending in .php to index

好久不见. 提交于 2019-12-11 21:05:41

问题


I've set up my .htaccess so far as follows:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [QSA]

My index.php loads the file from /pages, eg. index.php?page=home will load the page content from pages/home.php

However - if someone went to pages/home.php the page loads without the headers etc. which is undesirable. What can I add to redirect any URLs ending in .php to just take the user to the homepage?

Thanks


回答1:


A separate RewriteRule (without file exists condition) for ^(.*)\.php$ should work. But why are people trying to access the .php directly? If there isn't a good reason, a Deny in the directory is probably a better bet.

(And, as @Col. Shrapnel comments, beware include injection. I hope you're filtering your page names well.)

edit: include injection: Consider what happens if someone gives you an interesting page name, such as page=http://foo.com/exploit, will your script run it? What about page=/etc/passwd or page=/../../../etc/passwd, will you print out the password file? page=`mysqldump …`, will you give a copy of your database?




回答2:


This should redirect pages/anything.php to index.php?page=anything:

RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^(.*/)?([^/]*)\.php$ /index.php?page=$2 [R=301,L]

note the security tips above. include injection is bad..



来源:https://stackoverflow.com/questions/4619726/htaccess-redirect-any-urls-ending-in-php-to-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!