本文目录:
- 1、如何利用.htaccess文件对PHP网站或文件进行伪静态处理
- 2、ASP伪静态可以支持.htaccess吗?
- 3、如何设置伪静态规则
- 4、apache 伪静态如何隐藏index.php?
- 5、kangle支持.htaccess伪静态 吗
- 6、Centos下让nginx支持.htaccess文件实现伪静态的方法
如何利用.htaccess文件对PHP网站或文件进行伪静态处理
利用.htaccess文件对PHP网站进行伪静态处理要考虑两点:
一.服务器支持伪静态,比如Apache要开启mod_rewrite模块支持
二.利用.htaccess完成伪静态需要根据不同的网站系统进行不同的设置,以下是一些常见系统的.htaccess设定:
01.wordpress:
BEGIN WordPress
IfModule mod_rewrite.c
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
/IfModule
02.Phpwind
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)-htm-(.*)$ $1.php?$2
RewriteRule ^(.*)simple/([a-z0-9\_]+\.html)$ $1/simple/index.php?$2
03.Discuz
RewriteEngine On
Rewrite 系统规则请勿修改
RewriteRule ^archiver/((fid|tid)-[0-9]+\.html)$ archiver/index.php?$1
RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1page=$2
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$1extra=page\%3D$3page=$2
RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2
RewriteRule ^tag-(.+)\.html$ tag.php?name=$1
04.ecshop
RewriteEngine On
direct one-word access
RewriteRule ^index\.html$ index\.php [L]
RewriteRule ^category$ index\.php [L]
access any object by its numeric identifier
RewriteRule ^feed-c([0-9]+)\.xml$ feed\.php\?cat=$1 [L]
RewriteRule ^feed-b([0-9]+)\.xml$ feed\.php\?brand=$1 [L]
RewriteRule ^feed\.xml$ feed\.php [L]
RewriteRule ^category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$ category\.php\?id=$1brand=$2price_min=$3price_max=$4filter_attr=$5page=$6sort=$7order=$8 [QSA,L]
RewriteRule ^category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)(.*)\.html$ category\.php\?id=$1brand=$2price_min=$3price_max=$4filter_attr=$5 [QSA,L]
RewriteRule ^category-([0-9]+)-b([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$ category\.php\?id=$1brand=$2page=$3sort=$4order=$5 [QSA,L]
RewriteRule ^category-([0-9]+)-b([0-9]+)-([0-9]+)(.*)\.html$ category\.php\?id=$1brand=$2page=$3 [QSA,L]
RewriteRule ^category-([0-9]+)-b([0-9]+)(.*)\.html$ category\.php\?id=$1brand=$2 [QSA,L]
RewriteRule ^category-([0-9]+)(.*)\.html$ category\.php\?id=$1 [QSA,L]
RewriteRule ^goods-([0-9]+)(.*)\.html$ goods\.php\?id=$1 [QSA,L]
RewriteRule ^article_cat-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$ article_cat\.php\?id=$1page=$2sort=$3order=$4 [QSA,L]
RewriteRule ^article_cat-([0-9]+)-([0-9]+)(.*)\.html$ article_cat\.php\?id=$1page=$2 [QSA,L]
RewriteRule ^article_cat-([0-9]+)(.*)\.html$ article_cat\.php\?id=$1 [QSA,L]
RewriteRule ^article-([0-9]+)(.*)\.html$ article\.php\?id=$1 [QSA,L]
RewriteRule ^brand-([0-9]+)-c([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)\.html brand\.php\?id=$1cat=$2page=$3sort=$4order=$5 [QSA,L]
RewriteRule ^brand-([0-9]+)-c([0-9]+)-([0-9]+)(.*)\.html brand\.php\?id=$1cat=$2page=$3 [QSA,L]
RewriteRule ^brand-([0-9]+)-c([0-9]+)(.*)\.html brand\.php\?id=$1cat=$2 [QSA,L]
RewriteRule ^brand-([0-9]+)(.*)\.html brand\.php\?id=$1 [QSA,L]
RewriteRule ^tag-(.*)\.html search\.php\?keywords=$1 [QSA,L]
RewriteRule ^snatch-([0-9]+)\.html$ snatch\.php\?id=$1 [QSA,L]
RewriteRule ^group_buy-([0-9]+)\.html$ group_buy\.php\?act=viewid=$1 [QSA,L]
RewriteRule ^auction-([0-9]+)\.html$ auction\.php\?act=viewid=$1 [QSA,L]
05.phpcms
RewriteEngine On
RewriteRule ^(.*)content-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/index\.php\?m=contentc=indexa=showcatid=$2id=
$3page=$4
RewriteRule ^(.*)show-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/index\.php\?m=contentc=indexa=showcatid=$2id=
$3page=$4
RewriteRule ^(.*)list-([0-9]+)-([0-9]+).html$ $1/index\.php\?m=contentc=indexa=listscatid=$2page=$3
ASP伪静态可以支持.htaccess吗?
可以,用kangle反代asp插件。
IIS平台并不支持.htaccess,IIS只支持 httpd.ini ,Apache服务器才支持 .htaccess ;
如果你的ASP不是运行在Apache平台上,你可以将.htaccess规则转换成httpd.ini规则。
如何设置伪静态规则
设置伪静态规则的具体步骤如下:
以IIS10 下设置伪静态规则为例:
1、首先需要安装Url重写模块,百度搜索,下载完安装,URL Rewrite Module。
2、然后重启机器,可以在iis全局看到URL rewrite模块。
3、然后点击要设置伪静态规则的站点,双击“URL 重写”图标。
4、然后进入“URL 重写”界面,点击右侧的“导入规则”。
5、进入“导入 mod_rewrite”界面,点击配置文件下面框最后的三个点。
6、找到伪静态规则文件:.htaccess,选中并点击打开。
7、回到“导入 mod_rewrite”界面,点击“导入”规则。
8、最后,点击右栏的“应用”按钮。
9、至此伪静态配置完成,iis上的伪静态规则是写在web.confg,在网站根目录上web.confg已写上相关程序规则
apache 伪静态如何隐藏index.php?
Apache伪静态配置方法有两种:
在网站根目录下添加文件,.htaccess,在此文件内写伪静态规则,使用它需要在apache配置文件http.conf中完成相应配置.具体配置方法网上很多,这里不再赘述.
而另一种是直接在httpd.conf中使用.这种方法性能高于前者。
应用场景
(1),采用.htaccess的场景
用户需要针对特定目录改变服务器的配置而又没有root权限的情况下,可以通过.htaccess文件实现。
如果服务器管理员不愿意频繁修改配置,则可 以允许用户通过.htaccess文件自己修改配置. 比如ISP,在同一个机器上运行了多个用户站点,希望用户可以自
己改变配置,便可以配置好apache,开启对.htaccess文件的支持即可.
(2),不采用.htaccess的场景
虽然上述步骤1可以实现,但一般都应该尽可能地避免使用.htaccess文件。任何希望放在.htaccess文件中的配置,都可以放在主配置文件(http.conf)的段中,而且更高效。
(3)避免使用.htaccess文件有两个主要原因
首先是性能。如果AllowOverride启用了.htaccess文件,则Apache需要在每个目录中查找.htaccess文件,因此,无论是否真正用到,启用.htaccess都会导致
性能的下降。另外,对每一个请求,都需要读取一次.htaccess文件。
还有,Apache必须在所有上级的目录中查找.htaccess文件,以使所有有效的指令都起作用,所以,如果请求/www/htdocs/example中的页面,Apache必须查找以下文件:
/.htaccess
/www/.htaccess
/www/htdocs/.htaccess
/www/htdocs/example/.htaccess
总共要访问4个额外的文件,即使这些文件都不存在。(注意,这可能仅仅由于允许根目录”/”使用.htaccess ,虽然这种情况并不多。)
其次是安全。这样会允许用户自己修改服务器的配置,这可能会导致某些意想不到的修改,所以请认真考虑是否应当给予用户这样的特权。但是,如果给予用
户较少 的特权而不能满足其需要,则会带来额外的技术支持请求,所以,必须明确地告诉用户已经给予他们的权限,说明AllowOverride设置的值,并引导他
们参阅相应的说明,以免日后生出许多麻烦。
注意,在/www/htdocs/example目录下的.htaccess文件中放置指令,与在主配置文件中段中放置相同指令,是完全等效的。
总结
由上我们可以知道,使用.htaccess文件具有性能上的不利之处,所以我们应该尽可能避免使用.htaccess文件,下面介绍一种不使用.htaccess文件进行伪静态
的方法:
Apache 1.x 的用户请检查 conf/httpd.conf 中是否存在如下两段代码:
LoadModule rewrite_module libexec/mod_rewrite.so
AddModule mod_rewrite.c
Apache 2.x 的用户请检查 conf/httpd.conf 中是否存在如下一段代码:
LoadModule rewrite_module modules/mod_rewrite.so
如果存在,且以开头,请删除。然后在配置文件(通常就是 conf/httpd.conf或者conf/extra/httpd-vhosts.conf)中加入如下代码。此时请务必注意,如果网
站使用通过虚拟主机来定义,请务必加到虚拟主机配置段 中去,如果加在虚拟主机配置外部将可能无法使用。改好后然后将 Apache 重启。
例
①.在apache配置文件httpd.conf中找到:
LoadModule rewrite_module modules/mod_rewrite.so
这句,去掉前边的注释符号“”,如果没有则添加这句。
②.在代码:
Options FollowSymLinks
AllowOverride None
下面添加伪静态代码(以Wordpress伪静态为例):
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
其中/usr/local/apache/htdocs/wwwtest即为要进行伪静态的网站的根目录
③.重启apache
kangle支持.htaccess伪静态 吗
kangle是支持htaccess伪静态的。
htaccess伪静态规则
IfModule mod_rewrite.c
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|samples|robots\.txt|views|plugins|backup|upload|images|install|admin123|plug|test|install|make|js|favicon\.ico|templates|mqqt|scl|app)
RewriteRule ^(.*)$ index.php/$1 [L]
/IfModule
Centos下让nginx支持.htaccess文件实现伪静态的方法
Centos下让nginx支持.htaccess文件实现伪静态的方法在Google上搜索的资料很多人都说nginx目前不支持.htaccess文件,我按照nginx的规则试验了一下,结果发现
nginx是完全支持.htaccess文件的!方法如下:1.
在需要使用.htaccess文件的目录下新建一个.htaccess文件,如我的一个Discuz论坛目录:vi
/var/www/html/bbs/.htaccess
2.
在里面输入规则,我这里输入Discuz的伪静态规则:
nginx
rewrite
rulerewrite
^(.*)/archiver/((fid|tid)-[w-]+.html)$
$1/archiver/index.php?$2
last;rewrite
^(.*)/forum-([0-9]+)-([0-9]+).html$
$1/forumdisplay.php?fid=$2page=$3
last;rewrite
^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$
$1/viewthread.php?tid=$2extra=page%3D$4page=$3
last;rewrite
^(.*)/profile-(username|uid)-(.+).html$
$1/viewpro.php?$2=$3
last;rewrite
^(.*)/space-(username|uid)-(.+).html$
$1/space.php?$2=$3
last;rewrite
^(.*)/tag-(.+).html$
$1/tag.php?name=$2
last;
end
nginx
rewrite
rulewq保存退出。3.
修改nginx配置文件:
vi
/etc/nginx/nginx.conf4.
在需要添加伪静态的虚拟主机的server{}中引入.htaccess文件,include
/var/www/html/bbs/.htaccessinclude
/var/www/html/bbs/.htaccess;(把这个改成你.htaccess文件的具体位置)wq保存退出。5.
重新加载nginx配置文件:/etc/init.d/nginx
reload重新打开网页看看,如果伪静态正常就证明你的rewrite
rule语法是正确的。
本文来源:https://www.yuntue.com/post/166153.html | 云服务器网,转载请注明出处!