昨天有要求说是网站上面添加伪静态,因为用的是cms,所以像TP,Laravel,这些框架的伪静态是不可以适用到CMS上面的。所以就在晚上找了一圈,这个CMS的伪静态可真难写呀,真的要自己一个一个写出来的,可能还有更容易的办法吧,勿喷。只是记录一下自己遇到的问题,希望下次适用的时候可以有的看,也希望可以帮助到一些人,对这个东西懂的大佬的话,就不用看了,看了也可能觉得垃圾。
接下来直接上解决办法吧,我先是在本地apache环境下配置了一下。试试水:
一,Apache Web Server(虚拟主机用户)
只需要在.htaccess文件写入即可,没有.htaccess文件的自己创建即可
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^index.html$ index.php
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^about-([0-9]+)-([0-9]+)\.html$ about.php?cid=$1&page=$2
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^news-([0-9]+)-([0-9]+)\.html$ news.php?cid=$1&page=$2
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^newsshow-([0-9]+)-([0-9]+)-([0-9]+)\.html$ newsshow.php?cid=$1&id=$2&page=$3
</IfModule>
应该就是这么简单了,自己看着自己的规则往上套用就可以了。
二, IIS7 Web Server(独立主机用户)
只需要在web.config文件写入即可
<?xml version="1.0" ?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="index">
<match url="^(.*/)*index.html$" />
<action type="Rewrite" url="{R:1}/index.php" />
</rule>
<rule name="about">
<match url="^(.*/)*about-([0-9]+)-(\w+)-([0-9]+)\.html$" />
<action type="Rewrite" url="{R:1}/about.php\?cid={R:2}&sid={R:3}&page={R:4}" />
</rule>
<rule name="news">
<match url="^(.*/)*news-([0-9]+)-([0-9]+)\.html$" />
<action type="Rewrite" url="{R:1}/news.php\?sid={R:2}&page={R:3}" />
</rule>
<rule name="newsshow">
<match url="^(.*/)*newsshow-([0-9]+)-([0-9]+)-([0-9]+)\.html$" />
<action type="Rewrite" url="{R:1}/newsshow.php\?cid={R:2}&id={R:3}&page={R:4}" />
</rule>
</rules>
</rewrite>
#下面这三行就是别的配置了,和伪静态没有关系,我是直接从我的项目拷贝下来的
<defaultDocument configSource="web_config\default.config"/>
<httpErrors configSource="web_config\httpErrors.config"/>
<handlers configSource="web_config\php.config"/>
</system.webServer>
</configuration>
1,还有 Nginx Web Server 写在nginx.conf 里面即可,但是具体的写法,自己研究吧,配置站点之后应该会生成 nginx.conf 文件,自己看看里面的规则就可以写了
2,IIS Web Server (独立主机用户) 对应的文件 httpd.ini
3,Apache Web Server(独立主机用户) 对应的文件 conf/httpd.conf
上面提到的规则就得根据自己的实际来写了,这样写没有一点问题的。希望可以帮到更多的人,不要像我一样,研究好久。