3

I've created a simple php file to display info fetched from my MYSQL database. Right after that i use a rewrite rule in Nginx to make the link seo friendly and to mask the .php file that fetches it.

Ex: http://localhost/myfile.php?seo=how-to-install-linux After rewrite rule the i can access it like:

http://localhost/how-to-install-linux

My rewrite rules are:

location / {
rewrite ^/([a-zA-Z0-9_$\-]+)$ /myfile.php?seo=$1 last;
rewrite ^/(.*)/$ /$1 permanent; <- just to block trailing slash
}

My problem is that i also want to block any direct access to my php file and i want only the seo friendly url to work.

location = /myfile.php {
deny all;
}

This rule blocks complete access to my php file, including through seo friendy url. Is there a way to make it work for the seo friendly version using NGINX? My other settings are:

location ~ \.php$ {
try_files $uri =404;
include fcgi.conf;
fastcgi_pass unix:/var/run/ajenti-v-php7.0-fcgi-drnou-php7.0-fcgi-0.sock;

}

I only use Nginx, no Apache installed.

1 Answer 1

4

You can use the internal directive to prevent a location from being accessed directly. See this document for details.

For example:

location = /myfile.php {
    internal;
    include fcgi.conf;
    fastcgi_pass unix:/var/run/ajenti-v-php7.0-fcgi-drnou-php7.0-fcgi-0.sock;
}
Sign up to request clarification or add additional context in comments.

1 Comment

It works ! Thanks man. I've searched on Google with basic search terms but haven't found nothing useful. Again, thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.