htaccess限制最大长度变量

时间:2014-07-02 11:16:35

标签: regex apache .htaccess mod-rewrite

首先这已经有效,但我想限制一些变量长度。 我的代码的任何其他改进也是受欢迎的

RewriteEngine on 
Options +FollowSymlinks
RewriteBase / 

RewriteRule pict/([a-z]+)/([0-9]+) retpic.php?type=$1&id=$2 [L]

RewriteRule pc/(.*) pcs/$1 [L]

RewriteRule ^([a-z]+)/([0-9]+) index.php?ln=$1&id=$2 [L]

RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !=/retpic.php
RewriteCond %{REQUEST_URI} !^/pcs/.*$
RewriteCond %{REMOTE_HOST} !^11\.11\.11\.11

RewriteRule . index.php [L]

retpic.php将我的照片分发到: ([a-z]+)是关于图片大小和类型的2通道指标 ([0-9]+)是我想要的图片的6位数字

是否可以将这些限制为最多2个和6个字符?

我的索引也一样: ([a-z]+)一个2字母语言指标是(en,de,li,nl) ([0-9]+)要显示的页面的6位数ID。

pcs文件夹包含多级文件夹结构中的css和js文件 并且应该可以免费使用

我使用的最后一部分将每个错误的url重定向到我的索引,除了我的ow ip地址 所以我可以访问我的cms

1 个答案:

答案 0 :(得分:0)

您可以使用范围正则表达式构造{...}强制长度:

RewriteEngine on 
Options +FollowSymlinks
RewriteBase / 

RewriteRule pict/([a-z]{2})/([0-9]{6})/?$ retpic.php?type=$1&id=$2 [L,QSA]

RewriteRule pc/(.*)$ pcs/$1 [L]

RewriteRule ^([a-z]{2})/([0-9]{6})/?$ index.php?ln=$1&id=$2 [L,QSA]

RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !=/retpic.php
RewriteCond %{REQUEST_URI} !^/pcs/.*$
RewriteCond %{REMOTE_HOST} !^11\.11\.11\.11
RewriteRule . index.php [L]