简体   繁体   English

如何在 Url 中隐藏.php 扩展

[英]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.很抱歉,如果有人问过这个问题,但似乎没有找到解决办法,上面的 .htaccess 代码对我有用,而我正在使用 windows,就在最近,我移植到 ZEDC9F0A5A5D57797BF68E3731 和我自己安装了很多, 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. 默认情况下,Apache禁止使用.htaccess文件应用重写规则,因此,首先,您需要允许对该文件进行更改。 Open the default Apache configuration file using nano or your favourite text editor. 使用nano或您喜欢的文本编辑器打开默认的Apache配置文件。

$ 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. 要使这些更改生效,请重新启动Apache。

$ sudo systemctl restart apache2

Now, create the .htaccess file in the web root. 现在,在Web根目录中创建.htaccess文件。

$ 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. 现在,您有了一个可操作的.htaccess文件,可用于重写规则。

Following line will work on centos/apache以下行将适用于 centos/apache

Options +Indexes +Includes +FollowSymLinks +MultiViews -ExecCGI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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