1

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!

Oscar
  • 125
  • 1
  • 9
  • Instead of `mod_rewrite`, you can use [`MultiViews`](https://httpd.apache.org/docs/2.4/content-negotiation.html#multiviews) – Phil Oct 08 '20 at 08:14
  • @anubhava, it looks hidding the extension name can work now on home page after adding the rule those rules in my apach2.conf. But, when I jump from home to login.php, the extension name appears again – Oscar Oct 08 '20 at 14:19
  • check this one: https://stackoverflow.com/questions/10028025/how-can-i-use-htaccess-to-hide-php-url-extensions/10031576#10031576 – anubhava Oct 08 '20 at 14:29

2 Answers2

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]
Nico Bleiler
  • 475
  • 4
  • 14
0

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]
Googlian
  • 6,077
  • 3
  • 38
  • 44