(转)透过URL Rewrite实现链接静态化(PHP)
(转)通过URL Rewrite实现链接静态化(PHP)
我们知道搜索引擎对于静态页面是非常友好的,因此很多网站通过生成静态页面等手段方便爬虫抓取自己网站的内容。但是有时候一些应用并不适合全部静态化,比如数据变化非常大的论坛/贴吧系统,这时候我们可以通过URL重写来实现链接的伪静态化,即网站对外使用静态化的链接,而内部实际上仍然使用动态页面的URL形式。比如像这样一个链接:http://www.ci123.com/abc.php?action=a&id=1,我们可以改写成http://www.ci123.com/abc/a/1.html的形式。这是搜索引擎优化最重要的内容之一,它还有一个额外的好处,可以使页面有一个永久链接,即便以后网站系统内部链接有变化,通过适当改变Rewrite规则就可以保证原先的外部URL一直有效。
下面介绍2种简单的Apache+PHP下实现URL重写的方法,第一种适合有服务器配置权限的用户,第二种适合租用空间的用户,也作为我近期的学习心得的整理。
1、对于有服务器配置权限的用户,推荐使用Apache的mod_rewrite模块,这里假设已经安装好mod_rewrite模块。打开Apache的配置文件,找到相应主机的部分,添加以下代码:
RewriteEngine On
RewriteRule ^/abc/([a-z]+)/([0-9]+).html$/abc.php?action=$1&id=$2 [L]然后在shell里执行service httpdreload,让Apache重新载入配置就好了。现在在PHP页面里面我们可以把链接写成abc/a/1.html的形式,Apache在解析这个URL的时候会rewrite成abc.php?action=a&id=1的形式,并返回正确的页面。运用正则表达式我们可以实现几乎任何我们想要的链接形式,mod_rewrite模块的功能异常强大,这里只是一个及其简单的应用。
2、对于租用空间的用户,一般都没有办法修改Apache的配置,这里有个变通的方法,原理是这样的:当要传递参数访问PHP页面时,正常情况下是通过自动全局变量$_GET来获得,比如上面的链接,在页面里可以通过$_GET['action'] 和$_GET['id']来获得,重写URL后就不行了。现在在每个页面里require一个url_rewrite.php文件,里面代码如下:
$filename = basename($_SERVER['SCRIPT_NAME']);
if (strtolower($filename) == "abc.php") {
if (!empty($_GET[id])) {
$id = intval($_GET[id]);
$action = intval($_GET[action]);
}
else {
$nav = $_SERVER["REQUEST_URI"];
$script = $_SERVER["SCRIPT_NAME"];
$nav = ereg_replace("^$script", "", urldecode($nav));
$vars = explode("/", $nav);
$action = intval($vars[1]);
$id = intval($vars[2]);
}
}
这样$action和$id也得到了,页面里链接可以写成abc.php/a/1的形式来访问相应页面。
需要注意的是这种方法效率较第一种低,第一种方法是在WEB服务器URL解析过程中实现的,而这里是在PHP页面解析过程里实现的,第2种方法只是变通,不得已而为之,要修改链接形式很不方便也不灵活。
欢迎大家一起交流讨论!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl
