您现在的位置是:首页 > PHP类库

李清波 2014-08-17 PHP类库 51206 复制当前网址

apache配置伪静态实例 重写技术

首先:

必须要空间支持 Rewrite 以及对站点目录中有 .htaccess 的文件解析,才有效.

如何让空间支持Rewrite 和 .htaccess 的文件解析呢 往下看

第一步:要找到apache安装目录下的httpd.cof文件,在里面找到

注意此处更改的AllowOverride none为这儿的内容

#
    # 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't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    #
    Options FollowSymLinks
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All
    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all

所以更应该搞清楚了再改

 

Options FollowSymLinks
 AllowOverride none

把none改all,
第二步:找到以下内容:

#LoadModule rewrite_module modules/mod_rewrite.so

改为

LoadModule rewrite_module modules/mod_rewrite.so

第三步:保存重启apache。

ok。

其次是.htaccess的书写规则:

.htaccess加入以下内容

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)list-id([0-9]+)\.html$ $1/company/search.php?sectorid2=$2
RewriteRule ^(.*)cominfo-([a-z0-9]+)\.html$ $1/member/index.php?uid=$2&type=cominfo
RewriteRule ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1/plus/list.php?typeid=$2&PageNo=$3
RewriteCond %{HTTP_HOST} ^[a-z0-9\-]+\.lujin\.com$
RewriteCond %{HTTP_HOST} !^(www|bbs)\.lujin\.com$
RewriteRule ^/?$ /%{HTTP_HOST}
RewriteRule ^/([a-z0-9\-]+)\.lujin\.com/?$ /member/index.php?uid=$1 [L]

对上面的一些解释

RewriteRule ^(.*)list-id([0-9]+)\.html$ $1/company/search.php?sectorid2=$2


这条是把企业库的分类进行伪静态处理
原先假设访问地址为http://www.xxx.com/company/search.php?sectorid2=1
现在地址为http://www.xxx.com/list-id1.html

优点:1、伪静态处理加速搜索引擎收入
2、地址映射到根目录,增加权重,提高排名
3、也不知道还有什么……

RewriteRule ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1/plus/list.php?typeid=$2&PageNo=$3


这个是供求等分类的列表,和上面的原理类似,到页面相应修改即可

下面是会员空间的二级域名方案
前提是把域名设置好泛解析

RewriteCond %{HTTP_HOST} ^[a-z0-9\-]+\.lujin\.com$
RewriteCond %{HTTP_HOST} !^(www|bbs)\.lujin\.com$
RewriteRule ^/?$ /%{HTTP_HOST}
RewriteRule ^/([a-z0-9\-]+)\.lujin\.com/?$ /member/index.php?uid=$1 [L]

假设原先的地址为http://www.xx.com/member/index.php?uid=admin
现在地址为http://admin.xx.com
你只要在你想出现的地方放上这个就可以,比如在列表页面可以用[field:writer/]获取用户名,那么就可以在列表页面直接链接会员空间,可以写成http://[field:writer/].xx.com

其他伪静态原理和上面类似。

 R[=code](force redirect) 强制外部重定向
强制在替代字符串加上http://thishost[:thisport]/前缀重定向到外部的URL。如果code不指定,将用缺省的302 HTTP状态码。

 F(force URL to be forbidden)禁用URL,返回403HTTP状态码。

 G(force URL to be gone) 强制URL为GONE,返回410HTTP状态码。

 P(force proxy) 强制使用代理转发。

 L(last rule) 表明当前规则是最后一条规则,停止分析以后规则的重写。

 N(next round) 重新从第一条规则开始运行重写过程。

 C(chained with next rule) 与下一条规则关联
如果规则匹配则正常处理,该标志无效,如果不匹配,那么下面所有关联的规则都跳过。

 T=MIME-type(force MIME type) 强制MIME类型

 NS (used only if no internal sub-request) 只用于不是内部子请求

 NC(no case) 不区分大小写

 QSA(query string append) 追加请求字符串

 NE(no URI escaping of output) 不在输出转义特殊字符
例如:RewriteRule /foo/(.*) /bar?arg=P1%3d$1 [R,NE] 将能正确的将/foo/zoo转换成/bar?arg=P1=zed

 PT(pass through to next handler) 传递给下一个处理
例如:
RewriteRule ^/abc(.*) /def$1 [PT] # 将会交给/def规则处理
Alias /def /ghi

 S=num(skip next rule(s)) 跳过num条规则

 E=VAR:VAL(set environment variable) 设置环境变量

其次要学会阅读apache日志文件

例如:D:/EmpireServer/php/apache2.2/logs/error.log该文件为调试的重要文件及其另一文件

例如: D:/EmpireServer/php/apache2.2/logs/access.log具体视自己相应的内容文件而修改


文章来源:http://www.liqingbo.com/blog-271.html

评论