繁体   English   中英

mod_rewrite和if子句

[英]mod_rewrite and if clause

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

可以使用以下规则完成:

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

这不会影响以下任何URL:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM