【问题标题】:Rewrite Mod Giving Internal Server Error When Trying to Rewrite LinksRewrite Mod 在尝试重写链接时给出内部服务器错误
【发布时间】:2013-05-01 14:58:29
【问题描述】:

有很多问题讨论重写模组问题。我读过它们,但没有一个能解决我独特的问题。我已经做了 3 个小时的研究来解决它,但我仍然卡住了。

我想重写通过file_get_contents() PHP 函数从远程站点检索到的源代码中的链接。

当我得到源代码时,链接结构是:

<a href='javascript:openWindow("index1.php?option=com_lsh&view=lsh&event_id=148730&tv_id=850&tid=34143&channel=0&tmpl=component&layout=popup&Itemid=335","735","770")'  >Link#1</a>

我想改写成:

<a href='javascript:openWindow("http://remotesite.com/index1.php?option=com_lsh&view=lsh&event_id=148730&tv_id=850&tid=34143&channel=0&tmpl=component&layout=popup&Itemid=335","735","770")'  >Link#1</a>

经过一些研究,我认为 rewrite mod 可以解决问题。我试图将下面的代码放在我的 .htaccess 文件中:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index1\.php?option - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule  http://remotesite/index1.php?option [L]

但是,它给了我内部服务器错误。

我在这里做错了什么?有没有其他方法可以按照上面描述的方式重写链接结构?

【问题讨论】:

  • 我不认为使用 .HTACCESS 对你来说是最好的解决方案。我认为使用正则表达式匹配和字符串替换是最好的。
  • 我知道,但现在我上面的 htaccess 代码出了什么问题
  • 那么除了htaccess之外最好的方法是什么

标签: php html .htaccess


【解决方案1】:

您无法匹配重写规则中的查询字符串。此外,mod_rewrite 无法重写您的内容。您需要使用某种反向代理,例如 mod_proxy_html 来动态重写您的内容。重写引擎仅在服务器收到请求时应用,因此您可以做的嵌套是在请求到达您的服务器(具有 htaccess 文件的服务器)后重定向(或使用 P 标志的反向代理)。

您拥有的任何规则都不会导致 500 内部服务器错误,但它们无论如何都不会起作用,因为您无法匹配重写规则中的查询字符串,此外,规则需要模式和目标。您的第二条规则只有一个没有模式的目标。最有可能的是,未加载 mod_rewrite 会导致 500 内部服务器错误。除此之外,请检查您的错误日志。

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index1\.php http://remotesite/index1.php [L,QSA,P]

【讨论】:

  • Jon Lin 先生,我已经尝试过您的代码以及同样的内部服务器错误
  • @user2338253 检查你的日志,你可能没有加载 mod_rewrite
  • @Mr Jon Lin 已经通过 phpinfo() 检查并显示 mod_rewrite 已加载
  • @user2338253 您的错误日志会说明问题所在,而不是您的 php 日志,而是网络服务器的错误日志。
【解决方案2】:

试试这个:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST}   !^www\.example\.com [NC]
    RewriteCond %{HTTP_HOST}   !^$
    RewriteRule ^/(.*)         http://www.example.com/$1 [L,R]
</IfModule>

【讨论】:

  • 艾哈迈德先生的代码没有内部服务器错误,但链接仍然没有变化
  • 您确定 mod_rewrite.c 已加载到您的 Apache 服务器中吗?通过 phpinfo() 测试它。并尝试将最后一行替换为:RewriteRule ^http://remotesite/index.php?(.*)$ index.php?$1 [QSA,L]
  • mod_rewrite.so 在 httpd.config 中未注释且处于活动状态,当我测试它时 py phpinfo() 我在加载的模块中发现了 mod_rewrite
  • 试试这个:RewriteCond %{HTTP_HOST} !^www\.remotesite\.com [NC] RewriteCond %{HTTP_HOST} !^$ RewriteRule ^/(.*) http://remotesite.com/$1 [L,R]
  • 仍然无法正常工作我认为我们应该离开 HtACCESS 方法并将我们的研究指向尝试在具有获取文件内容功能的情况下很好地实现它我可能使用 jquery 我在 jquery 的技能是 0
【解决方案3】:

经过 6 个多小时的研究,我设法通过 mod_rewrite 方法以外的方式解决了这个问题,这里是详细信息

诀窍很简单,我只是从获取文件内容方法更改为 curl 更多选项

下面是我使用的代码:

<?php
    //Get the url
    $url = "http://remotesite.com";

    //Get the html of url
    function get_data($url) 
    { 
       $ch = curl_init();
       $timeout = 5;
       //$userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US)AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.X.Y.Z Safari/525.13.";
       $userAgent = "IE 7 – Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)";
      curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
      curl_setopt($ch, CURLOPT_FAILONERROR, true);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_AUTOREFERER, true);
      curl_setopt($ch, CURLOPT_TIMEOUT, 10);
      curl_setopt($ch,CURLOPT_URL,$url);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
      $data = curl_exec($ch);
      curl_close($ch);
      return $data;

    }

    $html = file_get_contents($url);
    echo '<base href="http://remotesite.com/" />';
    echo $html;
?>

将每个相对路径更改为绝对路径的技巧在这里:

echo '<base href="http://remotesite.com/" />';

感谢@Jon lin、@ahmed 的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多