1

I have the following problem:

I want to remove .php and .html extention from url of my website.

The url is localhost/MySiteDir/MySitePage.php and I want to obtain localhost/MySiteDir/MySitePage.

I am following this:

https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/

& this

How to remove file extension from website address? guides.

So I created a .htaccess file on my MySiteDir and i put this code on this file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

But it doesn't work.

I have changed etc/apache2/sites-enabled/000-default.conf file adding

<Directory /var/www/html>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
  </Directory>

in this way

<VirtualHost *:80>



<Directory /var/www/html>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
  </Directory>

    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf


</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

and if in .htaccess i put

RewriteEngine on                          # enable url rewriting
RewriteCond %{REQUEST_FILENAME} !-d       # rewrite if the requested url is not a directory
RewriteCond %{REQUEST_FILENAME}\.php -f   # rewrite if the php file exists
RewriteRule ^(.*)$ $1.php [L]             # add .php to the url

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f  # rewrite if the html file exists
RewriteRule ^(.*)$ $1.html [L] 

when i try to access to my website i receive a 500 ERROR.

Community
  • 1
  • 1
  • What happens when you try to access `localhost/MySiteDir/MySitePage`? Do you get a 404 or does _"doesn't work"_ mean something else? Does it still work to access `localhost/MySiteDir/MySitePage.php`? – M. Eriksson Jan 10 '17 at 13:13
  • do you want to force a redirect to the url without extension or just be able to access the page without the extension ? – ᴄʀᴏᴢᴇᴛ Jan 10 '17 at 13:19
  • I get 404, and than, if i try localhost/MySiteDir/MySitePage.php, it works! Can this issue be caused by permissions?? – Diego Zucca Jan 10 '17 at 13:20
  • i want to delete .php and .html from all urls of my web app – Diego Zucca Jan 10 '17 at 13:21
  • there is a difference between *"allow access to pages without extension"* and *"redirect the user to a page without extension"* in the first case, if the user goes to `example.com/page` or `example.com/page.php` the page will be displayed the same and for the other case, if the user goes to `example.com/page.php` he will be redirected to `example.com/page` – ᴄʀᴏᴢᴇᴛ Jan 10 '17 at 13:36
  • Yes, i want the first case – Diego Zucca Jan 10 '17 at 13:47
  • You dont need all that code. just put the following line to your .htaccess **Options +Multiviews** – Amit Verma Jan 10 '17 at 14:04
  • Multiview is ok only if you are sure that you will never have a folder named the same as a file. if you have a folder `config` and a file `config.php` apache will always serve the php file – ᴄʀᴏᴢᴇᴛ Jan 10 '17 at 14:09
  • also, multiview can cause some `406 Not acceptable` errors because the rewriting relies on the accept header of the request. If the request sends `accept text/html` apache will not serve a php file since it is a `application/x-httpd-php` file (see : http://stackoverflow.com/q/16357933/3992945) – ᴄʀᴏᴢᴇᴛ Jan 10 '17 at 14:20
  • see my edit on the answer, i made a mistake : line comments must be on a "comment only" line and not after a directive – ᴄʀᴏᴢᴇᴛ Jan 10 '17 at 15:13

2 Answers2

1

Replace below code in .htaccess file

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

Remove .php extensions with .htaccess without breaking DirectoryIndex

Community
  • 1
  • 1
Sujal Patel
  • 592
  • 2
  • 5
  • 14
0

First, you need to be sure that the rewrite module is installed and enabled (see this question)

Once the rewrite module is enabled, make sure the apache config allows htaccess files (see the doc)

Then, you will need two rules for your rewrites one for PHP, the other for html :

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
Community
  • 1
  • 1
ᴄʀᴏᴢᴇᴛ
  • 2,939
  • 26
  • 44