Table of Contents
PHP如何实现网址伪静态,php伪静态
Home php教程 php手册 PHP如何实现网址伪静态,php伪静态

PHP如何实现网址伪静态,php伪静态

Jun 13, 2016 am 09:14 AM
Pseudo-static

PHP如何实现网址伪静态,php伪静态

 Apache的 mod_rewrite是比较强大的,在进行网站建设时,可以通过这个模块来实现伪静态。

 

主要步骤如下: 

  1.检测Apache是否开启mod_rewrite功能     可以通过php提供的phpinfo()函数查看环境配置,找到“Loaded Modules”,其中列出了所有apache2handler已经开启的模块,如果里面包括“mod_rewrite”,则已经支持,不再需要继续设置。如果没有开启“mod_rewrite”,则打开目录 apache目录下的“/apache/conf/” ,找到 httpd.conf 文件,再找到“LoadModule rewrite_module”,将前面的”#”号删除即表示取用该功能。    如果没有查找到“LoadModule” 区域,可以在最后一行加入“LoadModule rewrite_module ,modules/mod_rewrite.so”(独占一行),之后重启apache服务器。再通过phpinfo()函数查看环境配置就有“mod_rewrite”为项了。

   2.让apache服务器支持.htaccess    如何让自己的本地APACHE服务器支持:“htaccess”呢? 只需修改apache的httpd.conf设置就可以让 APACHE支持“.htaccess”了。打开 APACHE目录的CONF目录下的httpd.conf文件,找到: Options FollowSymLinks AllowOverride None 改为 Options FollowSymLinks AllowOverride All 就行了。

   3.建立.htaccess 文件    建立.htaccess文件时要注意,不能直接建,方法是通过记事本中的另存为菜单,在文件名窗口输入:“.htaccess”,然后点击保存。

   4.rewrite规则学习    在新建.htaccess文件之后,就在里面写入以下内容: RewriteEngine on #rewriteengine为重写引擎开关on为开启off为关闭 RewriteRule ([0-9]{1,})$index.php?id=$1 在这里,RewriteRule是重写规则,是用正则表达式的句子,([0-9]{1,})表示由数字组成的,$表示结束标志,表示以数字结束!如果要实现伪静态页面,规则如下: RewriteEngine on RewriteRule ([a-zA-Z]{1,})-([0-9]{1,}).html$index.php?action=$1&id=$2 在为个正则表达式中,([a-zA-Z]{1,})-([0-9]{1,}).html$是规则,index.php?action=$1&id=$2是要替换的格式,$1代表第1括号匹配的值,$2代表第二个括号的值,如此类推! 测试PHP脚本如下: index.php文件中的代码如下: echo ‘你的Action值为:’ . $_GET['action']; echo ‘ ’; echo ‘ID值为:’ . $_GET['id']; ?>

  在浏览器地址栏输入: localhost/page-18.html 输出的是: 你的Action值为:page ID值为:18

    呵呵,改写成功!

 

下面将我自己配置伪静态的一些个人经验分享一下:

  开始接触伪静态,看着很神奇,以为很难,其实很简单,就是在服务器中多配置几个虚拟域名,类似真正的访问网址。  只需三步

A.首先 在Apache  Apache模块中,开启 vhost_alias_moudle 

B.修改httpd.conf文件,在这个文件中,搜索rewrite  .找到  LoadModule rewrite_module modules/mod_rewrite.so    去掉前面的#

C.修改这个文件,在路径  D:\wamp\bin\apache\Apache2.2.21\conf\extra 下的httpd-vhosts.conf 文件,照着示例加一个就OK了


ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "d:/wamp/www/dz"
ServerName localhost.www.dz.cn
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common

D.在路径  C:\Windows\System32\drivers\etc 下 的 hosts 文件中配置一下

127.0.0.1 localhost.www.dz.cn   重启服务即可。              大功告成!

 ****************************************************************************  大家新年快乐!

注明:在浏览器输入中  localhost.www.dz.cn即可访问,我加一个localhost是为了和正式的网址加以区别。有不懂的可以QQ:1024900614联系我,方便大家一起探讨,共同进步!

   你的伪静态规则全部写在 .htaccess 文件中,由于已经开启了伪静态,会默认加载该文件。  写这个.htaccess 文件 要会写正则哦,学习一些正则基础吧!

 

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Let's talk about how to use pseudo-static to hide the php suffix Let's talk about how to use pseudo-static to hide the php suffix Mar 20, 2023 pm 06:46 PM

Pseudo-static refers to the technology of accessing dynamic URL addresses by disguising them as static addresses, while hiding the PHP suffix is ​​to modify the server configuration so that the PHP suffix is ​​no longer displayed when accessing dynamic pages. The advantage of this is that it can enhance the security of the website and avoid being cracked, while also making it more beautiful and increasing the user experience. This article will introduce in detail how to use pseudo-static to hide the php suffix to improve the security and user experience of the website.

Optimizing website SEO: practice of pseudo-static hiding php suffix Optimizing website SEO: practice of pseudo-static hiding php suffix Mar 07, 2024 pm 12:27 PM

As we all know, optimizing the SEO of a website is a very important part of website operation. The default URLs of dynamic web systems (such as PHP) used by many websites have extensions (.php, .html, etc.), which will affect the SEO effect of the website. In order to improve the optimization effect of the website, a common practice is to change the dynamic URL to a pseudo-static URL to hide the extension name and improve the user experience and search engine ranking of the website. This article will take "pseudo-static hidden php suffix" as the theme, introduce how to achieve this optimization in PHP websites, and

Improve website security: Pseudo-static rules implement PHP suffix hiding Improve website security: Pseudo-static rules implement PHP suffix hiding Mar 07, 2024 am 11:33 AM

Improve website security: Pseudo-static rules implement PHP suffix hiding. With the development of the Internet, website security issues have become increasingly prominent, including the prevention of malicious attacks and the protection of user data. An effective measure is to hide the PHP suffix through pseudo-static rules, which helps improve the security of the website and protect user privacy. In achieving this goal, we need to use some specific code examples to demonstrate how to achieve PHP suffix hiding. First, we need to understand what pseudo-static rules are. Pseudo-static is a method of converting dynamic web page links into

From principle to practice: detailed explanation of pseudo-static hidden php suffix From principle to practice: detailed explanation of pseudo-static hidden php suffix Mar 07, 2024 pm 03:27 PM

Title: From Principle to Practice: Detailed explanation of pseudo-static hidden PHP suffix In network development, in order to improve the security of the website and enhance the user experience, hiding the file extension in the URL has become a common operation. Among them, hiding the PHP file suffix is ​​a commonly used technical means, which can improve the security of the website, increase the aesthetics of the website, and is also beneficial to search engine optimization. This article will explain in detail the principle and practical operation of pseudo-static hiding PHP suffix, and provide specific code examples. 1. The principle of pseudo-static hiding PHP suffix pseudo-static

Using ThinkPHP6 to achieve pseudo-static Using ThinkPHP6 to achieve pseudo-static Jun 20, 2023 pm 11:59 PM

With the rapid development of the Internet, website construction has attracted more and more attention. As we all know, optimizing the SEO of a website can improve the ranking and traffic of the website, and pseudo-static is an integral part of the SEO optimization of the website. In this article, we will use ThinkPHP6 to implement pseudo-static and further explore the optimization and implementation process of pseudo-static. What is Pseudo-Static state? Before explaining pseudo-static implementation, let’s first understand what pseudo-static is. Pseudo-static is a method of rewriting the URL address of a web page to make it appear

Detailed explanation of how to turn off pseudo-static code in PHP Detailed explanation of how to turn off pseudo-static code in PHP Mar 24, 2024 pm 03:12 PM

Detailed explanation of how to turn off pseudo-static code in PHP With the continuous development of website development, pseudo-static code has become an important part of optimizing website links and improving user experience. Sometimes, we also need to turn off pseudo-static code, perhaps for debugging or other needs. In this article, we will discuss in step-by-step detail how to turn off pseudo-static code in PHP and provide concrete code examples. Understanding pseudo-static code First, let us briefly understand what pseudo-static code is. Pseudo-static code refers to the use of URL rewriting technology to convert dynamic

Tips for turning off pseudo-static code in PHP Tips for turning off pseudo-static code in PHP Mar 23, 2024 pm 03:12 PM

Title: Tips for turning off pseudo-static code in PHP Pseudo-static code refers to a technique that makes dynamically generated URLs look like static page links. When developing websites using PHP, sometimes we encounter situations where we need to turn off pseudo-static code, such as during the debugging phase or to solve some URL redirection problems. This article will share some tips for turning off pseudo-static code and provide specific code examples. 1. The method to turn off the pseudo-static code is to turn off the .htaccess file and find .ht in the root directory of the website.

A closer look at pseudo-static: how to properly hide php suffixes A closer look at pseudo-static: how to properly hide php suffixes Mar 08, 2024 am 10:15 AM

In the current era of rapid changes in network technology, the security and stability of websites have attracted more and more attention. Among them, hiding the real technical framework of the website has become one of the focuses of many webmasters. Pseudo-static technology is a commonly used method that can effectively improve the security of a website and help prevent the site from being attacked by malicious programs such as crawlers. This article will delve into how to correctly hide the php suffix in pseudo-static technology and provide specific code examples. 1. The concept of pseudo-static. Pseudo-static, that is, pseudo-static, refers to hiding the website in the URL address.

See all articles