Home > Enterprise >  Nginx Friendly URL File Extension
Nginx Friendly URL File Extension

Time:07-07

I got my nginx.conf file:

location ~*  \.(jpg|jpeg|png|gif|ico|css|js|swf)$ {
   expires 7d;
}

location /
{
    try_files $uri $uri/ @rewrite
}


location @rewrite
{
    rewrite ^/([a-zA-Z0-9_-] )?\/?([a-zA-Z0-9_-] )?\/?([a-zA-Z0-9_-] )?\/?([a-zA-Z0-9_-] )?\/?([a-zA-Z0-9_-] )?\/?([a-zA-Z0-9_-] )?$ /index.php?a=$1&b=$2&c=$3&d=$4&e=$5&f=$6 break;
}

I want to replace friendly-url (generated by php file) link with seo optimised one.

https://example.com/gallery/basic-friendly-url-here/photoid

File extension is required by search engine, so I need to make it look like this:

https://example.com/gallery/basic-friendly-url-here/photoid.png

Unfortunately, I can't do it this way, because nginx throws 404 error.

Any ideas how to redirect link https://example.com/gallery/* into executable php file located in /vendor/mindgoner/script.php and handle seo url as GET parameter, instead of path?

CodePudding user response:

Ok, if there are no physical directory named gallery under your web server root directory, you can use the following:

location ^~ /gallery/ {
    rewrite ^/gallery/(.*) /index.php?gallery_item=$1 last;
}

Then the requested filename will be available inside your index.php as the $_GET['gallery_item'].

  • Related