Home  >  Article  >  Backend Development  >  How to remove .php suffix from url in php

How to remove .php suffix from url in php

藏色散人
藏色散人Original
2021-10-28 09:46:183527browse

php去掉url里的.php后缀的方法:1、找到并打开apache下的“extra/httpd-vhosts.conf”文件;2、通过配置伪静态去掉php后缀即可。

How to remove .php suffix from url in php

本文操作环境:Windows7系统、PHP7.1版、DELL G3电脑

PHP怎么去掉url里的.php后缀?

比如这样一个链接

http://example.com/xxx.php

如何把它变成

http://example.com/xxx并在浏览器里可以访问

是用.htaccess么?怎么做?

方法:

做伪静态即可。

演示:

localhost/news.php?type=music&id=100 我们希望这个地址可以用下面的访问url来替换localhost/news-music-id100.html
具体做法是在apache下的extra/httpd-vhosts.conf下,进行如下设置:

<VirtualHost *:80>
DocumentRoot "C:/myenv/apache/htdocs/static2"
#Directory配置节点,用于指定该目录下的文件或是图片.的访问权限
#设置虚拟主机的错误页面,欢迎页面
ServerName www.hsp.com
<Directory "C:/myenv/apache/htdocs/static2">
#这里可以指定是否让人访问
#Allow from all
#是否列出文件目录结构
#如果希望列出 indexes不希望 none
#Options indexes
#如何配置网站的首页面
DirectoryIndex abc.html abc2.html
#如何配置404[x3] 错误页面,引导用户引入新页面
errorDocument 404 /404.html
#配置我们的rewrite规则
RewriteEngine On[x4]
#rewrite的规则 如果 aaa.html 就跳转到news.php
#$1 表示反向引用,第一个子表达式的内容
#说明如果在正则规范中直接引用子表达式的内容,则使用\n
#如果是在后面则使用$n
RewriteRule news-([a-zA-Z]+)-id(\d+)\.html$ news.php?type=$1[x5] &id=$2
</Directory>
</VirtualHost>

推荐学习:《PHP视频教程

The above is the detailed content of How to remove .php suffix from url in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn