mod_rewrite和if子句

时间:2013-12-31 06:35:52

标签: regex apache .htaccess mod-rewrite

RewriteEngine On
RewriteRule ^([^.]+)$ index.php?name=$1

我想使用像if子句这样的重写规则。 只有在斜杠后面的URL中有一个部分并且没有属性时才应该重定向。

website.com/article -> website.com/index.php?name=article
website.com/theme/artcile -> no redirection
website.com/article?any=attribute -> no redirection

1 个答案:

答案 0 :(得分:1)

可以使用此规则来完成:

RewriteEngine On

# make sure there is no query parameter
RewriteCond %{QUERY_STRING} ^$
# make sure there is nothing after a slash if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ index.php?name=$1 [L]

这将重写:

  1. website.com/article -> website.com/index.php?name=article
  2. website.com/article/ -> website.com/index.php?name=article
  3. 这不会影响任何这些网址:

    1. website.com/theme/artcile
    2. website.com/article?any=attribute