skip to Main Content

there are dozens of similar question in the internet (stackoverflow and other sites), but none of them worked for me.
I am trying to rewrite my URL to get rid of the .php extension at the end. I already tried the common rewrites in my config.
I hope somebody knows an answer i did not use yet.

2

Answers


  1. you can use .htaccess

    make a new file in the directory which your PHP files are there, make a new file called .htaccess, put in the following in there:

    RewriteCond %{THE_REQUEST} ^.*/*.php
    RewriteRule ^(.*).php$ /$1 [R=301,L]
    
    Login or Signup to reply.
  2. In your vhost config file or nginx.conf file, add the following lines in the inside server block :

    location / {
        try_files $uri $uri.html $uri/ @extensionless-php;
        index index.html index.htm index.php; }
    
    location ~ .php$ {
        try_files $uri =404; }
    
    location @extensionless-php {
        rewrite ^(.*)$ $1.php last; }
    

    Need to restart Nginx before use.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search