代码之家  ›  专栏  ›  技术社区  ›  different

修改包括标点符号?

  •  0
  • different  · 技术社区  · 17 年前

    .htaccess

    RewriteEngine on
    RewriteRule ^([^/\.]+)/?$ index.php?section=$1 [L]
    RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&page=$2 [L]
    RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&page=$2&split=$3 [L]
    

    在这里,正则表达式将不匹配任何带句点的内容,如果用户试图直接访问文件,这是有意的。

    下面是一些常见URL的示例。

    games/Spider:+The+Secret+of+Bryce+Manor
    games/Orbital
    features/Interviewed:+Dennis+Sijnen+from+No+Monkeys
    news/all/4
    

    这个 split page

    games/Must.Eat.Birds
    

    因为我不是特别擅长mod rewrite或regex,所以我想要一个允许句号的解决方案。我知道它可能是一个更复杂的正则表达式,而我在这里已经超出了我的深度。

    谢谢你的帮助。

    2 回复  |  直到 17 年前
        1
  •  2
  •   Simon Groenewolt    17 年前

    [^/\.] 具有 [^/] --这样,它们将匹配所有不是“/”的对象,而不是所有不是“/”和“非”的对象

        2
  •  2
  •   gnarf    17 年前

    Simons response将解决您的问题,只需将“页面”部分替换为 [^/] 而不是 [^/\.]

    根据您的问题文本,您可能会对一点mod_rewrite魔术感兴趣:这部分重写将匹配DocumentRoot中的任何真实文件、链接或目录-如果先发生这种情况,它将不匹配您以后的规则-因此确保 /css/main.css 转到真实文件。然后,任何其他内容都会被重写并转储到index.php中。

    # -s = File Exists
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    # -l = Is a SymLink
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    # -d = Is a Directory
    RewriteCond %{REQUEST_FILENAME} -d
    # if we match any of the above conditions - serve the file.
    RewriteRule ^.*$ - [NC,L]
    
    # adding your rules with the slight modifications pointed out above and by simon.
    # only allows '.' in the "page" portion.
    RewriteRule ^([^/.]+)/?$ index.php?section=$1 [L]
    RewriteRule ^([^/.]+)/([^/]+)/?$ index.php?section=$1&page=$2 [L]
    RewriteRule ^([^/.]+)/([^/]+)/([^/.]+)/?$ index.php?section=$1&page=$2&split=$3 [L]