【问题标题】:Use same variable name in htaccess for rewriting PHP page在 htaccess 中使用相同的变量名来重写 PHP 页面
【发布时间】:2015-02-26 10:46:42
【问题描述】:

我有两个页面index.phppage_detail.php,我在.htaccess 中使用以下代码进行重写:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /page_detail.php?cat=$1&title=$2&pageId=$3&id=$4 [L]
RewriteBase /
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /page_detail.php?countryname=$1&country=$2&id=$3 [L]

当我在 index.php 页面上时,URL 重写工作得很好。当我移至 detail_page.php 时,每个 href 在页面 URL 之后显示以下内容:

首页网址(运行良好):example.com/country/586/3.html

问题:example.com/country/586/country/586/3.html

HTML链接代码:

<a href="<?=country_name($con,$row['country'])?>/<?=$row['country']?>/<?=$row['id']‌​?>.html">

【问题讨论】:

  • 我认为这也与 HTML/PHP 代码中的路径有关,能否请您将代码发布到 href 链接所在的位置?
  • 是的,当然..
  • 实际上,它删除了最后一个参数值并将其视为baseurl并抛出url
  • 最好在链接中使用绝对 URL。

标签: .htaccess url rewriting


【解决方案1】:

您需要使链接看起来像这样:

<a href="/<?=country_name($con,$row['country'])?>/<?=$row['country']?>/<?=$row['id']‌​?>.html">

(注意前导/

或将其添加到页面标题:

<base href="/" />

【讨论】:

  • 想一想,如何添加许多具有相同长度变量的重写url。 RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /page_detail.php?cat=$1&catId=$2&id=$3 [L] 和 RewriteRule ^( [^/]*)/([^/]*)/([^/]*)\.html$ /page_detail.php?catname=$1&productId=$2&id=$3 [L]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-05
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 2021-05-14
  • 1970-01-01
相关资源
最近更新 更多