Flat URL 使用 .htaccess 重写

Flat URL Rewriting using .htaccess

我正在努力缩短我的关注度URL

来自

 www.domain.com/index_1.php?var=value
 www.domain.com/index_2.php?name=value

 www.domain.com/value
 www.domain.com/name/value

我到底是什么,

 www.domian.com                    // This should reach index_1.php
 www.domain.com/value              // This should reach index_1.php
 www.domain.com/name               // This should reach index_2.php
 www.domain.com/name/value         // This should reach index_2.php
 www.domain.com/name/              // The last slash should be omitted and redirect to "www.domain.com/name"

"value"范围可能包含[0-9][A-Z][a-z][-][_]

我试过遵循 .htaccess 代码,

 Options +FollowSymlinks
 RewriteEngine On
 RewriteBase /
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule (^name?$)|(^name/([^/]+)/?$) /index_2.php?name= [L]
 RewriteRule ^([^/][^(name)]+)/?$ /index_1.php?var= [L]

当我输入

 www.domian.com                    // It Works.
 www.domain.com/value              // It shows "404 Page Not Found".
 www.domain.com/name               // It works.
 www.domain.com/name/value         // It works.
 www.domain.com/name/              // It shows "404 Page Not Found".

如果可以使用 PHP 完成,请回答。

您可以使用:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

# For www.domain.com/value
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index_1.php?var= [L,QSA]

# For www.domain.com/name/value
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ index_2.php?= [L,QSA]

你要区分www.domain.com/namewww.domain.com/value.

否则,您可以使用 PHP.

来完成此操作
Options +FollowSymlinks
RewriteEngine On
RewriteBase /

# For www.domain.com/value
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?var= [L,QSA]

# For www.domain.com/name/value
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([\w-]+)/([\w-]+)/?$ index_2.php?= [L,QSA]

在index.php中:

if(isset($_GET["var"])) {
  $value = $_GET["var"];
  if($value == "name"){
    include"index_2.php";  
  }else{
    include"index_1.php";
  }
}else{
  include"index_1.php";
}