htaccess的动态模式重写规则

时间:2015-05-10 00:44:24

标签: .htaccess mod-rewrite url-rewriting

有人可以帮我为我的.htaccess制定匹配规则吗?

我希望旧的网址在搜索引擎上编入索引,如:

http://www.domain.com/watch/ANY_ID

自动重定向到:

RewriteEngine On
RewriteRule ^watch/([^/]*)$ /watch.php?id=$1 [L]

编辑:

我试过了:

Options +FollowSymLinks
RewriteEngine on
RewriteRule watch/(.*) watch.php?id=$1

和此:

class test{
  static function Unix($arr){return implode("/",$arr);}
  static function Windows($arr){return implode("\\",$arr);}
  function test(){
    $slash = "Unix";
    echo self::$slash(["path","to","a","folder"]);
  }
}

但是不适合我。

1 个答案:

答案 0 :(得分:0)

您只包含在内部映射/重写新URI的代码。如果您还想将旧URI重定向到新的onw,那么您可以在/.htaccess文件中使用以下内容:

RewriteEngine on

# Prevent loops
RewriteCond %{ENV:REDIRECT_STATUS} !200

# Check for old URI and redirect to new URI
RewriteCond %{QUERY_STRING} ^id=([\d]+)$
RewriteRule ^watch.php$ /watch/%1? [R,L]

# Map new URI to file
RewriteRule watch/(.*) /watch.php?id=$1 [L]