8

I'm trying to clean urls in NGINX using framework DooPHP.

This => - http://example.com/index.php/something/more/ To This => - http://example.com/something/more/

I want to remove (clean url) the "index.php" from the url if someone try to enter in the first form. Like a permanent redirect.

How to do this config on NGINX?

Thanks.

[Update: Actual nginx config]

server {
  listen 80;
  server_name vip.example.com;
  rewrite ^/(.*) https://vip.example.com/$1 permanent;
}

server {
       listen 443;
       server_name vip.example.com;

       error_page 404 /vip.example.com/404.html;
       error_page 403 /vip.example.com/403.html;
       error_page 401 /vip.example.com/401.html;
       location /vip.example.com {
           root /sites/errors;
       }

       ssl on;
       ssl_certificate /etc/nginx/config/server.csr;
       ssl_certificate_key /etc/nginx/config/server.sky;

       if (!-e $request_filename){
           rewrite /.* /index.php;
       }

       location / {
           auth_basic  "example Team Access";
           auth_basic_user_file  config/htpasswd;
           root   /sites/vip.example.com;
           index  index.php;
       }

       location ~ \.php$ {
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  /sites/vip.example.com$fastcgi_script_name;
           include        fastcgi_params;
           fastcgi_param PATH_INFO $fastcgi_script_name;
      }

}

6 Answers 6

7

This was the line I was looking for, I did it my self. I could have some bugs.

What do you think?

server {

 ....

  rewrite ^/index.php/(.*) /$1  permanent;

 ....

}
3

I've try it on CodeIgniter 1.7 & 2.0, and i change

config.php $config['uri_protocol'] = "REQUEST_URI";

and i add try_files $uri $uri/ /index.php?r=$request_uri;

on nginx.conf, and worked

location / {
       auth_basic  "example Team Access";
       auth_basic_user_file  config/htpasswd;
       root   /sites/vip.example.com;
       index  index.php;
       try_files $uri $uri/ /index.php?r=$request_uri;
   }

maybe it same on DooPHP

1
  • Now try requesting /index.php explicitly.
    – AnrDaemon
    Aug 26, 2016 at 10:30
2

Add this to nginx server config part.

location / {
                if (!-e $request_filename) {
                        rewrite ^/([^?]*)(?:\?(.*))? /index.php?title=$1&$2 last;
                }
                if ($uri ~* "\.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$") {
                        expires max;
                        break;
                }
        }
0

Remove this line

if (!-e $request_filename){
   rewrite /.* /index.php;
}

Add this inside location /

    rewrite ^(.*)$ /index.php;

    if (!-e $request_filename){
        rewrite ^(.*)$ /$1.php;
    }

Hopefully, this work for you.

0

Add this location object to your server configuration in your nginx.conf. If location / already exists, then just add the try_files line.

location / {
    try_files $uri /index.php;
}
0

I have added the following rule to my NGINX config file (After the the all location rules):

if ($request_uri ~* "^(.*/)index\.php(.*)") {

  return 307 $1$2;

} 

It is working fine for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.