search
HomeBackend DevelopmentPHP Tutorial关于页面优化和伪静态_php技巧

关于页面优化和伪静态

1)版面优化
2)伪静态(重点涉及apache,smarty,正则)

详细内容:

一、版面优化:
版面优化其实主要涉及HTML,JS,CSS,XML之间的关系(XML相关在此不作描述).
1)一般来说,在资源共享的前提下,我们最基本的目的是让搜索引擎所收录(很多人被AJAX所迷惑,到处使用AJAX,但我的观点是,只有在后台或用户操作部分才使用).
因此,首先我们应该按搜索引擎的收录准则来设计(其实下面说的“伪静态”还不是为了搜索引擎,由于相关文档有好几个PAGE,请自行搜索),主要是html的使用问题,如

2)然后解决加载速度和内容纯度问题:
主要是以下几个原则:
1>不要为了版面美观,把无谓的HTML加上去,建议把版面美观的任务交给CSS,并认真考虑CSS的可重用性,HTML只作为对信息内容的描述(好像是XML的重点吧)。我在网上抽查了好一部分的站点,好的网站,html占总内容的50%以下,但有的站点,文字内容占总内容不到20%,
2>把JS,CSS写成文件.只要是利用了浏览器的CAHCHE,减少内容下载
3>HTML标签应该尽量减少嵌套,我见过夸张的一个站点,TABLE嵌套居然是11层..狂汗….

3)解决数据合理处理时间
这个涉及内容比较多,主要是

二、伪静态
这里主要描述apache,smarty的应用,当然,其实使用什么模板甚至不使用模板都没什么关系的,只是笔者长年使用smarty,深浓感受到它的强大
该部分主要针对的是对系统有控制权和对apache、正则较为熟悉的用户。

在这里,核心是强调正则的应用,如果你不会正则表达式,那么你就只能停留一成不变的抄袭阶段,甚至无法使用.而且正则在应用上普遍(基本上什么语言都有)、频繁和强大,笔者还是建议花点时间,学精一点,受用终生
对于搜索引擎,据我所知,关键是处理GET中的”?”、”&”.”php”,还有就是URL长度的问题就OK了,形式就看个人爱好了。
先说APACHE,关键是使用mod_rewrite,打开mod_rewrite模块(在httpd.conf中,把LoadModule rewrite_module modules/mod_rewrite.so前面的“#”去除)

如果使用了vhost(),可以在vhost里面加入类似下面的代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^xxx.com$
RewriteRule ^/([^\.\/]+)\.html$ /index.php?action=$1 [L]

解释:
以上配置不一定放在vhost里,按你个人要求放得合适就行。
第一行,表示该vhost将要使用rewrite(URL重写)
第二行,RewriteCond是用于如果后面条件符合(第一个参数满足第二个参数,其中第二个参数为正则表达式),则执行下面的RewriteRule指令,其中%{xxxx}表示是apache的变量,%{HTTP_HOST}表示URL的主机(域名),其它变量请查看apache2手册
第三行,实现url重写(重头戏),第一个参数为在浏览器中输入的url,满足该正则的uri才执行重写,第二个参数是重写规则,即把满足第一个参数的url 按照该规则转换成你须要的url在这里笔者必须指出,重写后的url如果包含”http://”,跳转后的地址会显示在浏览器的地址栏中。第三个参数是一些控制,如以上[L]表示该重写是最后一条,后面的重写规则不再被执行。

smarty部分:
主要是处理输出的页面内容,你使用apache的rewrite后,你页面中的链接当然使用了它的规则了,如:原来是 abc.php?action=doit就要改用类似abc/action-doit.html这样的方式表示,当然,你可以在做页面时自己手动去改,但我觉得这是比较笨的方法.为什么不去使用ob_xxxxx()去控制呢?(ob_xxxx()系的函数使用请参考php手册).在这里的介绍使用 smarty去代替,因为这样会更加灵活
在smarty中,使用register_outputfilter()注册一个处理方法即可,具体方法类似为:
//先定义好一个处理函数
function change_url($tpl_output, &$smarty)
{
$tpl_output=preg_replace(”/\/index.php?\?action=([^&]+)/i”,”/\\1.html”,$tpl_output);
return $tpl_output;
}
//该函数第一个参数是smarty的页面内容,第二个是smarty指针
//然后使用
$tpl->register_outputfilter(”change_url”);

register_outputfilter()方法是输出过滤函数,即交给 change_url($tpl_output, &$smarty)第一个参数是smarty处理后的页面内容
同类型的还有前过滤方法register_prefilter(),即把smarty模板交给第一个参数,详细使用方法请参考smarty手册

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
Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

What is the difference between PHP and ASP.NET?What is the difference between PHP and ASP.NET?Apr 28, 2025 pm 04:56 PM

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function