如何更改http:mydomainname.com/inner-page.php?articleid = 10到http:mydomainname.com/inner-page.php/Title

时间:2014-01-11 13:45:50

标签: .htaccess mod-rewrite

如何更改http:mydomainname.com/inner-page.php?articleid = 10到http:mydomainname.com/inner-page.php?articleid = Title
 请给我一个简单的方法,我也尝试过Apache mod_rewrite。

但我的困惑是如何.htaccess文件通过id知道我的标题

1 个答案:

答案 0 :(得分:1)

你无法单独使用mod_rewrite或Apache,因为Apache或mod_rewrite无法知道如何从"10"转换为"Title"。您最有可能希望通过数据库查找来执行此操作,并且最好在inner-page.php中执行此操作。

if( isset( $_GET['articleid'] ) ) {
  $title = database_lookup_function( $_GET['articleid'] );
  header( '302 Moved Temporarily' );
  header( "location: /inner-page.php?articletitle=$title" );
  exit();
}

或者如果你真的想让整个脚本保持相同的变量,这听起来很愚蠢:

if( isset( $_GET['articleid'] ) && intval( $_GET['articleid'], 10 ) == $_GET['articleid'] ) {
  $title = database_lookup_function( $_GET['articleid'] );
  header( '302 Moved Temporarily' );
  header( "location: /inner-page.php?articleid=$title" );
  exit();
}

要在inner-page.php?category=radiono&lanug=India&articleid=sarukh-khan-performs-a-na‌​tural-language请求时加载inner-page.php/radiono/India/sarukh-khan-performs-a-natural-language,请使用以下规则:

RewriteRule ^inner-page\.php/([^/]+)/([^/]+)/([^/]+)/?$ /inner-page.php?category=$1&lanug=$2&articleid=$3 [L]