简体   繁体   中英

How to Hide .php Extension in Url

# Protect My Directories
Options -Indexes
# Protect my htaccess file
<Files .htaccess>
order deny,allow
deny from all
</Files>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]

I'm sorry if this Question has been asked but don't seem to find a fix, The Above .htaccess code worked for me, while I was using windows, Just of recent, I ported to Linux and had to install pretty much everything myself, when I try viewing a web page with .php (extension) It shows, but without .php (extension) it shows a 404 HTTP error code. Any Idea as to why? and how to fix things?

By default, Apache prohibits using a .htaccess file to apply rewrite rules, so first, you need to allow changes to the file. Open the default Apache configuration file using nano or your favourite text editor.

$ sudo nano /etc/apache2/sites-available/000-default.conf

inside that file, you will find a block starting on the first line. Inside of that block, add the following new block so your configuration file looks like the following. Make sure that all blocks are properly indented.

<VirtualHost *:80>
<Directory /var/www/html>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
</Directory>

. . .
</VirtualHost>

Save and close the file. To put these changes into effect, restart Apache.

$ sudo systemctl restart apache2

Now, create the .htaccess file in the web root.

$ sudo nano /var/www/html/.htaccess

Add this line at the top of the new file to activate the rewrite engine.

RewriteEngine on

Save the file and exit.

You now have an operational .htaccess file which you can use to rewrite rules.

Following line will work on centos/apache

Options +Indexes +Includes +FollowSymLinks +MultiViews -ExecCGI

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM