2012-03-19 86 views
0

我正在重写我的问题。 下面我将详细介绍我正在尝试做的事情,以及我得到的结果是什么。我希望这次能够很清楚。我在这里有一个样本页面www.broadcast2world.com/samples.php。 我们在页面上看到的所有链接和数据都来自数据库。现在假设我们点击某个视频的链接“更多信息”来说明社交控制。该视频的具体href被触发为<a href="video.php?pid='.$id.'">。它通过其对URL ID格式http://www.broadcast2world.com/video.php?pid=66.无法通过.htaccess将动态网址转换为静态网址

我赶上ID为使用

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

使用$的pageid可变的变量,我运行一个查询,最终为特定的视频制作的页面。

我的搜索引擎优化人员需要的是www.broadcast2world.com/video/name-of-the-video格式的网址。

现在的问题是,如果我做了一个mod_rewrite修正,这是由Ben下面的奇妙解释,我无法弄清楚,如何将该名称的视频链接到它的ID。如上所述,我确信必须在创建视频的特定网址时修改一些内容。但我真的不知道是什么?再次感谢您精彩的支持

+0

有没有什么运气可以解决这个问题? – 2012-03-21 13:49:43

+0

嗨本,很多感谢后续行动。我还没有能够正确配置它。当我生成仍处于?查询格式的网址时,我想我错过了一些东西。我很害怕问这个问题,因为上次我做这个时,许多贡献者都投了弃权票,嘲笑我。如果我能告诉你我正在做的事情,是否可以? – 2012-03-26 10:50:10

+0

嗨,本,我明白你的观点。我正在重新编辑这个问题,我希望这次能让自己更清楚。感谢您的建议。 – 2012-03-26 15:05:03

回答

2

该网站生成的.htaccess代码有许多错误。

  • 句号(.)应在重写规则指令的左手侧被转义 - 即\.php
  • 查询字符串未被考虑(这是您的问题的主要原因,我在下面解释)。
  • 没有迹象表明使用的重定向类型。临时?常驻?
  • 没有RewriteBase指令。因为/被视为默认值,所以不重要,但为了便于阅读,我倾向于使用它。

重写指令特别只考虑了REQUEST_URI组件。以下是来自mod_rewrite manual page

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] 

输入网址: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 

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

要重复的规则,你可以复制开始的RewriteCond重写规则和线条。

1

替换以下行

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

随着下面的代码

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

注意更换/的一个?

同样做出同样的改变到第二次重写规则

+0

@Ben Swinburne:谢谢你。 – 2012-03-19 22:11:14