skip to Main Content

I am trying to remove the php or html’s extension name from the url, but after going through lots of posts on the Internet and tried the methods, but my url does not work as I expected. What I want is xxx.com/xxx/home, instead of xxx.com/xxx/home.php

My system config

  • Ubuntu 20.04.1 LTS remote server on aws

Here are the steps I tried so far:

  1. sudo a2enmod rewrite==>enable module rewrite
  2. sudo vim apache2.conf==>config the apache2.conf
<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>
  1. create a .htaccess file
Options -Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !.php$ [NC]
RewriteCond %{REQUEST_URI} [^/]$
RewriteRule ^(.*)$ $1.php [L]
  1. put the .htaccess on my diectory (/var/www/html/.htaccess)

Is there any wrong step I made miss any configuration? Could someone help with this issue? Thank you!

2

Answers


  1. Add this in the .htaccess to hide the php file extensions from the url

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^.]+)$ $1.php [NC,L]
    
    Login or Signup to reply.
  2. Try the below code in your .htaccess file

    # Run Php without filename extension
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php
    
    # Return 404 if original request is .php
    RewriteCond %{THE_REQUEST} "^[^ ]* .*?.php[? ].*$"
    RewriteRule .* - [L,R=404]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search