如何访问不带文件扩展名的网页-Apache .htaccess URL重写
在本教程中,我们将学习在不使用文件扩展名的情况下加载文件。
为此,我们将配置Apache服务器,并创建.htaccess文件。
文档根目录
文档根目录是托管服务器上的文件夹或者目录,所有网页和其他资产(例如图像文件,样式表和JavScript文件等)都保留其中。
当用户访问该时,她实际上正在访问该文件夹。
简而言之:该目录包含我们的。
Linux类型服务器上的文档根目录通常设置为/var/www/html
.htaccess文件
.htaccess是运行Apache的Web服务器的配置文件。
这是一种特殊的文件,通常用于URL重写,重定向到其他URL,阻止访问等。
场景
考虑我们的URL是https://www.example.com
让我们考虑一下,文档根目录包含以下文件。
/ | | +--api/ | | | | | +-- helloworld.php | | +-- index.php
因此,我们在Document Root文件夹中有一个api文件夹和一个index.php文件。
在api文件夹中,我们有一个helloworld.php文件。
现在,为了从Web浏览器访问index.php文件,我们可以输入以下URL。
https://www.example.com/index.php
并且,为了访问api文件夹中的helloworld.php文件,我们必须输入以下URL。
https://www.example.com/api/helloworld.php
注意!如果在浏览器中使用以下URL,则将收到错误消息,并且不会加载文件。
https://www.example.com/index
https://www.example.com/api/helloworld
如何加载不带文件扩展名的文件?
为了解决这个问题,我们必须在服务器端进行少量更改。
httpd.conf文件中的更改
通过终端使用SSH密钥或者用户名和密码登录到服务器。
您将在以下目录中找到httpd.conf文件。
/etc/httpd/conf
在终端中使用以下命令转到该目录。
[root@example ~]# cd /etc/httpd/conf
进入conf目录后,您将看到httpd.conf文件。
使用ls -la
命令。
[root@example conf]# ls -la
drwxr-xr-x 2 root root 4096 Jan 01 10:01 . drwxr-xr-x 5 root root 4096 Jan 01 10:01 .. -rw-r--r-- 1 root root 11776 Jan 01 10:02 httpd.conf
使用以下命令制作文件的备份副本。
[root@example conf]# cp httpd.conf httpd.conf.backup
如果要还原,制作备份文件很有用。
上面的命令将在conf文件夹中创建一个httpd.conf.backup文件。
[root@example conf]# ls -la
drwxr-xr-x 2 root root 4096 Jan 01 10:01 . drwxr-xr-x 5 root root 4096 Jan 01 10:01 .. -rw-r--r-- 1 root root 11776 Jan 01 10:02 httpd.conf -rw-r--r-- 1 root root 11753 Jan 01 10:10 httpd.conf.backup
现在,使用vi打开httpd.conf文件。
并确保您有权修改文件。
必要时使用sudo
。
[root@example conf]# vi httpd.conf
或者,如果您使用的是sudo。
[root@example conf]# sudo vi httpd.conf
这将在vi编辑器中打开httpd.conf文件。
现在向下滚动,直到找到这行。
<Directory "/var/www/html">
现在按i键切换到INSERT模式。
使用#哈希符号注释掉" AllowOverride None"行。
#AllowOverride None
现在,在其后写以下行。
AllowOverride All
现在,您将获得以下内容。
# Further relax access to the default document root: <Directory "/var/www/html"> # # Possible values for the Options directive are "None", "All", # or any combination of: # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews # # Note that "MultiViews" must be named *explicitly* --- "Options All" # doesn&apost give it to you. # # The Options directive is both complicated and important. Please see # http://httpd.apache.org/docs/2.4/mod/core.html#options # for more information. # Options Indexes FollowSymLinks # # AllowOverride controls what directives Jan be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # #AllowOverride None AllowOverride All # # Controls who can get stuff from this server. # Require all granted </Directory>
现在,按ESC键退出INSERT模式,然后执行:wq
保存文件并退出。
您将看到命令提示符。
输入以下命令以重新启动apache。
[root@example conf]# systemctl restart httpd
或者,如果需要,请使用sudo。
[root@example conf]# sudo systemctl restart httpd
现在.htaccess
现在,使用以下命令移至"文档根目录"。
[root@example conf]# cd /var/www/html
进入html(文档根目录)目录后,使用vi命令创建一个.htaccess文件。
[root@example html]# vi .htaccess
或者,如果需要,请使用sudo。
[root@example html]# sudo vi .htaccess
vi编辑器将打开。
按i键切换到INSERT模式并编写以下内容。
<IfModule mod_rewrite.c> #this will start rewrite engine RewriteEngine On #this will set the base directory RewriteBase / </IfModule>
现在,按ESC键退出INSERT模式,然后执行:wq
保存文件并退出。
因此,在上面的文件中,我们已经打开了重写引擎,现在文件应该打开而没有扩展名。
现在,在浏览器中使用以下URL访问不带文件扩展名的文件。
https://www.example.com/index
https://www.example.com/api/helloworld