Home  >  Q&A  >  body text

nginx - How to use re-rules to remove .php from URL

ThinkPHP How to use Apache and Nginx to rewrite rules when REWRITE mode is turned on,
Remove .php
in http://www.example.com/api.php/user/info/1880233 Becomes http://www.example.com/api/user/info/1880233

Looking for examples of rewrite rules for apache and nginx.

PHPzPHPz2713 days ago919

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 17:05:43

    Sorry, I didn’t see the question clearly before

    Nginx:

    location ~ .php {
      fastcgi_pass   127.0.0.1:9000;  #根据实际情况修改
      fastcgi_index  index.php;
      include        fastcgi_params;
    
      #这一段是让 Nginx 支持 PATH_INFO
      set $real_script_name $fastcgi_script_name;
      if ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") {
          set $real_script_name ;
          set $path_info ;
      }
      fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
      fastcgi_param SCRIPT_NAME $real_script_name;
      fastcgi_param PATH_INFO $path_info;
    }
    
    location / {
      if (!-e $request_filename) {
        rewrite ^/api/(.*)$ /api.php/ last;
        break;
      }
    }
    

    Apache:
    Still refer to the introduction in this link: http://doc.thinkphp.cn/manual/url_rewrite.html
    But change the code to the following:

    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^/api/(.*)$ api.php/ [QSA,PT,L]
    </IfModule>
    

    And put the .htaccess file into the api.php folder

    reply
    0
  • Cancelreply