标签:rewrite

浏览模式: 标准 | 列表 | 图文
lnmp组件中sablog的配置纠错

lnmp组件中sablog的配置纠错

lnmp是现在相对比较容易和被大家所接受的一个套装。由于我用的是Sablog所以我在设定vhost的时候就选了sablog,但是告诉我第二行出错了。

仔细检查了一下,好象确实是有这个问题,一来好象是编码也有问题,二来它copy的规则有点象是discuz的规则,和sablog系统后台中的规则不一样。所以,我根据 sablog后台中的规则进行了修正,立刻正常了。
sablog的默认规则是支持apache的rewrite的。如下:

# BEGIN Sablog-X
RewriteEngine On
RewriteBase /
# 文章
RewriteRule ^show-([0-9]+)-([0-9]+)\.shtml$ index.php?action=show&id=$1&page=$2
# 分类
RewriteRule ^category-([0-9]+)-([0-9]+)\.shtml$ index.php?action=index&cid=$1&page=$2
# 只带月份的归档
RewriteRule ^archives-([0-9]+)-([0-9]+)\.shtml$ index.php?action=index&setdate=$1&page=$2
# 用户列表、高级搜索、注册、登陆
RewriteRule ^(archives|search|reg|login|index|links)\.shtml$ index.php?action=$1
# 全部评论、标签列表、引用列表 带分页
RewriteRule ^(comments|tagslist|trackbacks|index)-([0-9]+)\.shtml$ index.php?action=$1&page=$2
# END Sablog-X

注意那个.shtml,那是我自定义的URL后缀,所以你看我的博客的时候,都有这玩意。。。将就点了。说实话,我怀疑这个shtml对SEO有友好度,所以我才这么配置(纯属猜测)
改完后配置是这样的:

location / {

rewrite ^/show-([0-9]+)-([0-9]+)\.shtml$ /index.php?action=show&id=$1&page=$2 last;
rewrite ^/category-([0-9]+)-([0-9]+)\.shtml$ /index.php?action=index&cid=$1&page=$2 last;
rewrite ^/archives-([0-9]+)-([0-9]+)\.shtml$ /index.php?action=index&setdate=$1&page=$2 last;
rewrite ^/(archives|search|reg|login|index|links)\.shtml$ /index.php?action=$1 last;
rewrite ^/(comments|tagslist|trackbacks|index)-([0-9]+)\.shtml$ /index.php?action=$1&page=$2 last;
}

其实看到没,与apache下的配置很相像,只是在最前面加了 ^/ ,然后在指向的文件前加 /
同时在最后加 last;
嗯,就这样。。。灰常方便,你换了没?

Yii Rewrite中遇到的困惑

Yii Rewrite中遇到的困惑

用Yii开发的时候,总是会有涉及到rewrite,否则,网址里一堆 index.php?r=xxxx那就真的太长了。

关于rewrite,可以参考官方的guide,不过上面只有针对apache的htaccess配置。如果是nginx,你还可以搜索一下官方的wiki,有介绍nginx的配置的。只是官方上面用的方法是try_file,只针对高版本的。如果是低版本还是得用 if (!e $request_filename)这类的【更多设置请搜索google】我这里不详细说了。

今天在用官方推出的这一种rewrite功能【Parameterizing Hostnames 】的时候,出了点问题,该功能是:

Parameterizing Hostnames

Starting from version 1.0.11, it is also possible to include hostname into the rules for parsing and creating URLs. One may extract part of the hostname to be a GET parameter. For example, the URL http://admin.example.com/en/profile may be parsed into GET parameters user=admin and lang=en. On the other hand, rules with hostname may also be used to create URLs with paratermized hostnames.

In order to use parameterized hostnames, simply declare URL rules with host info, e.g.:

array(
'http://<user:\w+>.example.com/<lang:\w+>/profile' => 'user/profile',
)

The above example says that the first segment in the hostname should be treated as user parameter while the first segment in the path info should be lang parameter. The rule corresponds to the user/profile route.

Note that CUrlManager::showScriptName will not take effect when a URL is being created using a rule with parameterized hostname.

Also note that the rule with parameterized hostname should NOT contain the sub-folder if the application is under a sub-folder of the Web root. For example, if the application is under http://www.example.com/sandbox/blog, then we should still use the same URL rule as described above without the sub-folder sandbox/blog.

我在rules里写上了 "http://user.xxx.com/<_c:\w+>/<_a:\w+>"=>"user/<_c>/<_a>/"

然后发现访问 http://user.xxx.com/aaa/bbb/id/1的时候出现了404错误,找问题死活找不到在哪里。再转过头去看了很多其他的配置,发现都不正常。

然后,我尝试访问默认页,即没有参数的:http://user.xxx.com/default/index,咦,居然正常了。。但问题随之而来,其他链接都不正常,经过不停的测试测试再测试。最终发现,把""user/<_c>/<_a>/" 中最后一个 / 去掉就正常了。

那么,'<_m:\w+>/<_c:\w+>/<_a:\w+>' => '<_m>/<_c>/<_a>/' 和 '<_m:\w+>/<_c:\w+>/<_a:\w+>' => '<_m>/<_c>/<_a>' 的区别是什么呢?

仔细看了一下生成的链接,如果用第二种的时候,如果有多个参数,就转为 m/c/a?a=b&c=d 而如果用第一种就是生成 m/c/a/a/b/c/d 。这时候,系统可能会识别不出我这种用二级域名做rewrite的方式。于是,我去掉了最后一个“/”,一切都正常了。

期间,HM和Horadric.Cube多次对我帮助,告诉我不同的nginx参数的配置。灰常感谢。

TIPS:利用rewrite来规避注入漏洞

TIPS:利用rewrite来规避注入漏洞

PHP的新手在开发的时候不可避免的会遇到SQL注入的问题,很多老手在介绍的时候是叮嘱叮嘱再叮嘱啊。其实有个简单的方法就可以减少这种问题的过失。
1、rewrite 规则
2、很多框架的路由
这两种方式都是将GET方法做了一个简单正则处理,也只有符合规则的情况下,GET值才会被赋值到变量中。如果将ID等做一个\d的判断,那么非数字的已经经过一次过滤了。
而且,你在采用rewrite规则的时候,很多sql语句就无法通过URL被传递进来,因为他们不能被正则正常解析。
当然,你也可以想象,如果你的URL是1-0-0-1-0-1-0-1-1-1.html你能知道这些位数是干嘛的不?这种情况用于搜索比较常见,因为搜索的条件会比较复杂,也会比较容易被用来做注入。
如果根据ID取文章之类的,你show-1-1.html别人是知道你的ID为多少,但已经不太容易注入了吧?
新手只是要学习一下rewrite规则了。了解一下正则也好HOHO

利用.htaccess绑定域名到子目录

利用.htaccess绑定域名到子目录

rewrite规则说白了,其实并不难,只要你了解了正则规范后,你就会发现,其实,写这东西,比写PHP的正则要容易的多。毕竟PHP的正则你要考虑很多东西。而用在URL上的,不可能太多。
下面的这篇文章是08年的,不过仍然可以作为参考。以前的uchome下面,好象也是支持三级域名的。也可以参考一下官方的配置,但。。。好象是找不到官方的rewrite规则?
二级域名。如果你真的不会设置,手头又没有啥资料,你可以安装一下wordpress多用户版,安装完毕后生成的.htaccess文件,你就可以拿来作为参考了。

前提
首先得把域名绑定绑定了,比如把dev.ccvita.com解析到211.136.108.190这个IP
其次是在网站管理面板里,为网站绑定dev.ccvita.com
最后编辑配置.htaccess文件,就可以了。如果没有开启.htaccess请访问下面的内容,《Apache之AllowOverride参数详解》:http://www.ccvita.com/281.html

.htaccess文件内容
RewriteEngine on
# 把 ccvita.com 改为你要绑定的域名.
RewriteCond %{HTTP_HOST} ^(www.)?ccvita.com$

# 把 subfolder 改为要绑定的目录.
RewriteCond %{REQUEST_URI} !^/subfolder/

# 不要改以下两行.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# 把 dev 改为要绑定的目录.
RewriteRule ^(.*)$ /dev/$1

# 把 ccvita.com 改为你要绑定的域名
# 把 dev 改为要绑定的目录.
# dev/ 后面是首页文件index.php, index.html
RewriteCond %{HTTP_HOST} ^(www.)?ccvita.com$
RewriteRule ^(/)?$ dev/index.php [L]

文中所涉及的域名,我没有更改过哦。原文来自:http://www.ccvita.com/362.html。
同样,如果你是typecho,如果你在后台打开rewrite规则后,你也可以参考一下(不过,这个只适合单用户,哪天要是typecho开发多用户版,估计现在这个.htaccess也是可以被拿来使用的,谁知道呢?)

ZF在ubuntu下的运行

ZF在ubuntu下的运行

ZEND FRAMEWORK要想正常的在ubuntu的AMP平台下运行 ,得打开apache的rewrite模块。这和windows下面把rewrite前的注释去掉可不一样。

进入命令行:a2enmod rewrite

会提示你成功打开rewrite模块

然后:

/etc/init.d/apache2 force-reload

重新加载一下,不过,这样还是不能成功的,因为默认的virtualhost中 有一个参数的值默认为NONE,要改成ALL

运行 :

vim /etc/apache2/sites-enabled/000-default

修改

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
把AllowOverride None 的 None 改为 All

然后

/etc/init.d/apache2 restart

OK,现在运行ZEND的例子,正常了吧,呵呵

apache rewrite 详解

apache rewrite 详解

懒得写上什么,apache的rewrite一向是最让人头疼的。这是我几年前找的文章,继续转贴一下,毕竟以前的BLOG我也不知道能够被保留多久, 这个毕竟是自己的,只要没啥意外,会一直保留着的。

http://www.infomall.cn/cgi-bin/mallnohead/20040514/http://hedong.3322.org/archives/000349.html

收藏自:竹笋炒肉
东坡有诗“无竹则俗,无肉则廋;不俗不廋,竹笋炒肉”。:)
欢迎光临的每一位朋友。这是我的第一个BLOG,用来记录我的所学、所做、所思、所想、所经历、所感受。

内容如下:

Apache的Mod_rewrite学习(五)

  今天主要列出一些例子。由于有些例子是针对特殊路径或特别情况的,列出供大家在思路上参考。因为它们就是些例子。

目标 重写设置 说明
规范化URL RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 [R] 将/~user重写为/u/user的形式
RewriteRule ^/([uge])/([^/]+)$ /$1/$2/ [R] 将/u/user末尾漏掉的/补上
规范化HostName RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name [NC] 域名不合格
RewriteCond %{HTTP_HOST} !^$ 不空
RewriteCond %{SERVER_PORT} !^80$ 不是80端口
RewriteRule ^/(.*) http://fully.qualified.domain.name:%{SERVER_PORT}/$1 [L,R] 重写
RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R]
URL根目录转移 RewriteEngine on
RewriteRule ^/$ /e/www/ [R] 从/移到/e/www/
末尾目录补斜线 RewriteEngine on
(目录范围内) RewriteBase /~quux/
RewriteRule ^foo$ foo/ [R] /~quux/foo是一个目录,补/
RewriteEngine on
RewriteBase /~quux/
RewriteCond %{REQUEST_FILENAME} -d 如果请文件名是个目录
RewriteRule ^(.+[^/])$ $1/ [R] URL末尾不是斜线时补上
Web集群 RewriteEngine on
RewriteMap user-to-host txt:/path/to/map.user-to-host 用户-服务器映射
RewriteMap group-to-host txt:/path/to/map.group-to-host 组-服务器映射
RewriteMap entity-to-host txt:/path/to/map.entity-to-host 实体-服务器映射
RewriteRule ^/u/([^/]+)/?(.*) http://${user-to-host:$1|server0}/u/$1/$2 用户均衡
RewriteRule ^/g/([^/]+)/?(.*) http://${group-to-host:$1|server0}/g/$1/$2 组均衡
RewriteRule ^/e/([^/]+)/?(.*) http://${entity-to-host:$1|server0}/e/$1/$2 实体均衡
RewriteRule ^/([uge])/([^/]+)/?$ /$1/$2/.www/
RewriteRule ^/([uge])/([^/]+)/([^.]+.+) /$1/$2/.www/$3\
URL根目录搬迁 RewriteEngine on
RewriteRule ^/~(.+) http://newserver/~$1 [R,L] 到其它服务器
所用户名首字母分 RewriteEngine on
RewriteRule ^/~(([a-z])[a-z0-9]+)(.*) /home/$2/$1/.www$3 内一层括号为$2
NCSA imagemap移 RewriteEngine on
植为mod_imap RewriteRule ^/cgi-bin/imagemap(.*) $1 [PT]
多目录查找资源 RewriteEngine on
# first try to find it in custom/...
RewriteCond /your/docroot/dir1/%{REQUEST_FILENAME} -f
RewriteRule ^(.+) /your/docroot/dir1/$1 [L]
# second try to find it in pub/...
RewriteCond /your/docroot/dir2/%{REQUEST_FILENAME} -f
RewriteRule ^(.+) /your/docroot/dir2/$1 [L]
# else go on for other Alias or ScriptAlias directives,
RewriteRule ^(.+) - [PT]
据URL设置环境变量 RewriteEngine on
RewriteRule ^(.*)/S=([^/]+)/(.*) $1/$3 [E=STATUS:$2]
虚拟主机 RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.host\.com$ 基于用户名
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^www\.([^.]+)\.host\.com(.*) /home/$1$2
内外人有别 RewriteEngine on
RewriteCond %{REMOTE_HOST} !^.+\.ourdomain\.com$ 基于远程主机
RewriteRule ^(/~.+) http://www.somewhere.com/$1 [R,L]
错误重定向 RewriteEngine on
RewriteCond /your/docroot/%{REQUEST_FILENAME} !-f 不是regular文件
RewriteRule ^(.+) http://webserverB.dom/$1
程序处理特殊协议 RewriteRule ^xredirect:(.+) /path/to/nph-xredirect.cgi/$1 \ Xredirect协议
[T=application/x-httpd-cgi,L]
最近镜像下载 RewriteEngine on
RewriteMap multiplex txt:/path/to/map.cxan 顶级域名与最近ftp服务器映射
RewriteRule ^/CxAN/(.*) %{REMOTE_HOST}::$1 [C]
RewriteRule ^.+\.([a-zA-Z]+)::(.*)$ ${multiplex:$1|ftp.default.dom}$2 [R,L] 据顶级域名不同提供不同的FTP服务器
基于时间重写 RewriteEngine on
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900
RewriteRule ^foo\.html$ foo.day.html 白天为早晚7点间
RewriteRule ^foo\.html$ foo.night.html 其余为夜间
向前兼容扩展名 RewriteEngine on
RewriteBase /~quux/
# parse out basename, but remember the fact
RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes]
# rewrite to document.phtml if exists
RewriteCond %{REQUEST_FILENAME}.phtml -f 如果存在$1.phtml则重写
RewriteRule ^(.*)$ $1.phtml [S=1]
# else reverse the previous basename cutout
RewriteCond %{ENV:WasHTML} ^yes$ 如果不存在$1.phtml,则保持不变
RewriteRule ^(.*)$ $1.html
文件改名(目录级) RewriteEngine on 内部重写
RewriteBase /~quux/
RewriteRule ^foo\.html$ bar.html
RewriteEngine on 重定向由客户端再次提交
RewriteBase /~quux/
RewriteRule ^foo\.html$ bar.html [R]
据浏览器类型重写 RewriteCond %{HTTP_USER_AGENT} ^Mozilla/3.*
RewriteRule ^foo\.html$ foo.NS.html [L]
RewriteCond %{HTTP_USER_AGENT} ^Lynx/.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[12].*
RewriteRule ^foo\.html$ foo.20.html [L]
RewriteRule ^foo\.html$ foo.32.html [L]
动态镜像远程资源 RewriteEngine on
RewriteBase /~quux/
RewriteRule ^hotsheet/(.*)$ http://www.tstimpreso.com/hotsheet/$1 [P] 利用了代理模块
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^usa-news\.html$ http://www.quux-corp.com/news/index.html [P]
反向动态镜像 RewriteEngine on
RewriteCond /mirror/of/remotesite/$1 -U
RewriteRule ^http://www\.remotesite\.com/(.*)$ /mirror/of/remotesite/$1
负载均衡 RewriteEngine on 利用代理实现round-robin效果
RewriteMap lb prg:/path/to/lb.pl
RewriteRule ^/(.+)$ ${lb:$1} [P,L]
#!/path/to/perl
$| = 1;
$name = "www"; # the hostname base
$first = 1; # the first server (not 0 here, because 0 is myself)
$last = 5; # the last server in the round-robin
$domain = "foo.dom"; # the domainname
$cnt = 0;
while (<STDIN>) {
$cnt = (($cnt+1) % ($last+1-$first));
$server = sprintf("%s%d.%s", $name, $cnt+$first, $domain);
print "http://$server/$_";
}
##EOF##
静态页面变脚本 RewriteEngine on
RewriteBase /~quux/
RewriteRule ^foo\.html$ foo.cgi [T=application/x-httpd-cgi]
阻击机器人 RewriteCond %{HTTP_USER_AGENT} ^NameOfBadRobot.*
RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.[8-9]$
RewriteRule ^/~quux/foo/arc/.+ - [F]
阻止盗连你的图片 RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://www.quux-corp.de/~quux/.*$ [NC] 自己的连接可不能被阻止
RewriteRule .*\.gif$ - [F]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !.*/foo-with-gif\.html$
RewriteRule ^inlined-in-foo\.gif$ - [F]
拒绝某些主机访问 RewriteEngine on
RewriteMap hosts-deny txt:/path/to/hosts.deny
RewriteCond ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND} !=NOT-FOUND [OR]
RewriteCond ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND} !=NOT-FOUND
RewriteRule ^/.* - [F]
用户授权 RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend1@client1.quux-corp\.com$
RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend2@client2.quux-corp\.com$
RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend3@client3.quux-corp\.com$
RewriteRule ^/~quux/only-for-friends/ - [F]
外部重写程序模板 RewriteEngine on
RewriteMap quux-map prg:/path/to/map.quux.pl
RewriteRule ^/~quux/(.*)$ /~quux/${quux-map:$1}
#!/path/to/perl
$| = 1;
while (<>) {
s|^foo/|bar/|;
print $_;
}
搜索引擎友好 RewriteRule ^/products$ /content.php
RewriteRule ^/products/([0-9]+)$ /content.php?id=$1
RewriteRule ^/products/([0-9]+),([ad]*),([0-9]{0,3}),([0-9]*),([0-9]*$) /marso/content.php?id=$1&sort=$2&order=$3&start=$4

参考文献:
URL Rewriting Guide
http://httpd.apache.org/docs/misc/rewriteguide.html

mod_rewrite: A Beginner's Guide to URL Rewriting
http://www.sitepoint.com/article/910