如何用htaccess显示在URL中传递的2个参数中的1个?

时间:2014-02-17 11:41:31

标签: php apache .htaccess mod-rewrite wampserver

目前我有一些重定向的.htaccess代码

localhost/category.php?id=something&phrase=whatever 

localhost/id/phrase

我现在要做的是添加额外的代码,这些代码只会在URL中显示其中一个名为article.php的不同页面的参数所以它只是

localhost/article/id

请注意,我希望删除上述URL中的短语参数,并且只显示要显示的id部分。这是我目前在.htaccess文件中的完整代码:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# external redirect from /category.php?id=name&phrase=something to /name/something
RewriteCond %{THE_REQUEST} \s/+category\.php\?id=([^&]+)&phrase=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L]

# skip files and directories from rewrites
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# Cleans up all the article, and wrestler pages
RewriteRule ^/?article/([^/]+)/?$ article.php?id=$1 [L,QSA]
RewriteRule ^/?wrestler/([^/]+)/?$ wrestler.php?id=$1 [L,QSA]

# Removes the .php extension from pages
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteRule ^([^/]+)/([^/]+)/?$ category.php?id=$1&phrase=$2 [L,QSA]

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

大多数人都这样做:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ router_script.php?/$1 [L]

然后他们的路由器脚本执行类似的操作:

$args = explode('/', $_SERVER['QUERY_STRING']);
switch($args[0]) {
  case 'articles':
    $_GET['id'] = isset($args[1]) ? $args[1] : '';
    include "articles.php";
  break;
}

那里有更优雅的方法,但这可能会成功。