无法通过.htaccess将动态url转换为静态url


not able to convert dynamic url to static url via .htaccess

我正在重写我的问题。下面我给出了我想要做的所有细节,以及我得到的结果。我希望这次能说得很清楚。我在这里有一个样本页面www.broadcast2world.com/samples.php。我们在页面上看到的所有链接和数据都来自数据库。现在假设我们点击一个链接"更多信息"为一个特定的视频说社会控制。该视频的特定href被触发为<a href="video.php?pid='.$id.'">。它以http://www.broadcast2world.com/video.php?pid=66. 格式通过url传递其id

我使用将该id捕获到一个变量中

if(!isset($_GET['pid'])){
    $pageid='66';
}
else{
  $pageid=$_GET['pid'];  
} 

使用$pageid变量,我运行一个查询,最终为该特定视频准备页面。

我的SEO人员需要的是一个格式为www.broadcast2world.com/video/name-of-the-video.的url

现在的问题是,如果我做了一个mod_rewrite修改,Ben在下面精彩地解释了这一点,我就不知道如何将视频的名称链接到它的id。我相信在为视频创建特定url时,我必须修改一些内容,如上所述。但我真的不知道是什么?再次感谢您对的大力支持

该网站生成的.htaccess代码有很多问题。

  • 句点字符(.)应在RewriteRule指令的左侧转义,即'.php
  • 查询字符串没有被考虑在内(这是问题的主要原因,我将在下面解释)
  • 没有指示使用的重定向类型。短暂的永久
  • 没有RewriteBase指令。不重要,因为默认使用/,但为了可读性,我倾向于使用它

重写指令特别只考虑REQUEST_URI组件。以下内容来自mod_rewrite手册页面。

REQUEST_URI请求的URI的路径组件,例如"/index.html"。这明显排除了查询字符串可用作其自己的名为QUERY_STRING的变量。

下面的代码在注释中进行了解释,下面可以找到这里的调试信息。

# Enable rewrite engine http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteengine
RewriteEngine On
# Sets the base URL for per-directory rewrite http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
RewriteBase /
# Match the regular expression ^pname=...$ with the QUERY_STRING variable as the subject
# Query string starts with pname (`^`). 
# Matches exactly pname=overviewvideos.
# Must end with overviewvideos (`$`)
RewriteCond %{QUERY_STRING} ^pname=overvidevideos$
# If the above condition is met, redirect to the 2nd parameter.
# Note the escaped period ('.)
# Note the status code 302 (Found, temporary) Can be 301 for permanent etc.
# Note the L flag - this prevents further rules being processed if the conditions are met. (L for Last)
# Note that at this stage we are back to matching the REQUEST_URI, so just allvideos.php in our regular expression. Must start and end with the string allvideos.php
RewriteRule ^allvideos'.php$ allvideos.php?pname=Overview%20Videos [R=302,L]

输入URL:http://www.yoursite.com/allvideos.php?pname=overvidevideos

RewriteCond %{QUERY_STRING} ^pname=overvidevideos$  This condition was met
RewriteRule ^allvideos'.php$ allvideos.php?pname=Overview%20Videos [R=302,L]    
    This rule was met, the new url is http://www.yoursite.com/allvideos.php?pname=Overview%20Videos
    Test are stopped, because of the R in your RewriteRule options. A redirect will be made with status code 302

输出URL:http://www.yoursite.com/allvideos.php?pname=Overview%20Videos

若要重复这些规则,您可以重复以"RewriteCond"answers"RewriteRule"开头的行。

替换以下行

RewriteRule ^allvideos.php/pname=overviewvideos$ allvideos.php?pname=Overview%20Videos 

使用以下代码

RewriteRule ^allvideos.php?pname=overviewvideos$ allvideos.php?pname=Overview%20Videos 

注意用? 替换/

类似地,对第二个重写规则进行相同的更改