Nginx Redirect and Hide /index.php and /index.htm and /index.html in URLs
By Shay Anderson on November 2013
Sometimes it is useful to not allow an end-user direct access to URLs like /index.php or /index.htm or /index.html. For example, the URL /index.php should not be accessible if / outputs the same response as /index.php.
We can easily tell nginx to redirect all /index.php, /index.htm or /index.html to / (and for all longer path requests, for example /mypath/index.php). Edit your nginx config file for your HTTP (or HTTPS) server and add the following rewrite rule: server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.htm;
# add rewrite rule here:
# block access to /index.(php|htm|html)
if ($request_uri ~ “/index.(php|html?)”) {
rewrite ^ /$1 permanent;
}
location / {
…
}
…
}Now your nginx Web server will hide/redirect the /index.(php|htm|html) requests.