【问题标题】:Got stuck in a very basic .htaccess rewrite陷入了一个非常基本的 .htaccess 重写
【发布时间】:2012-09-29 19:04:52
【问题描述】:
<h1><a href="profile.php?id=' . $id . '" target="_self">' . $name. '</a></h1>

以上是指向我的 profile1.php 文件的引用。该文件称为 index.php 。它目前显示的网址是这样的:

http://www.domain.com/interact/profile1.php?id=36

我已经尝试实现 .htaccess 文件来重写 url。我尝试了很多组合,其中大多数都给出了 500 错误,有些没有重写 url。 这是我使用的 .htaccess 文件,它不会改变 url。

我希望网址看起来像 http://www.domain.com/interact/profile/36

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile1.php/$1 [QSA,L]
</IfModule>

我知道这是一个非常基本的问题,但我似乎坚持并阅读了基本教程但无法正确实施。

文件 index.php 、profile1.php 和 .htaccess 在名为 interact 的文件夹中。

告诉我需要对 php 或 .htaccess 文件进行的任何更改。

【问题讨论】:

  • "我已经尝试实现 .htaccess 文件来重写 url",是什么?目前尚不清楚您要做什么或您的问题是什么。是you're confusing rewrite and redirect吗?
  • @JonLin 我忘了写网址。我已经用我想重写的新网址编辑了问题。

标签: .htaccess mod-rewrite url-rewriting


【解决方案1】:

它目前显示的网址是这样的:http://www.domain.com/interact/profile1.php?id=36

...

我希望网址看起来像 http://www.domain.com/interact/profile/36

第 1 步:

将您的内容更改为具有这样的链接:

<h1><a href="profile/' . $id . '" target="_self">' . $name. '</a></h1>

这样,当您单击链接时,URL 地址栏中将显示的 URL 将如下所示:http://www.domain.com/interact/profile/36

第 2 步:

那么你需要使用这些规则在内部将它改回来:

RewriteEngine On
RewriteBase /interact/
RewriteRule ^profile/([0-9]*) profile1.php?id=$1 [L,QSA]

为了将任何外部链接(例如 Google 索引机器人)指向新的 URL,您还需要添加这些链接:

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /interact/profile1\.php\?id=([0-9]*)
RewriteRule ^ http://www.domain.com/interact/profile/%2 [L,R=301]

【讨论】:

  • 好的,这对页面来说很好,并且正在重写 url,但它弄乱了我的 css。我的 css 文件与 profile1.php 或 index.php 不在同一目录中。它位于 www.domain.com/css。如何使 css 正确。
  • @hritikwatts 您需要在个人资料页面的标题中添加&lt;base href="/interact/"&gt;
  • 使用您给 RewriteBase /interact/ 的行,它给出了所需的 url,但给出了 object not found 错误,但在删除该行时它工作正常。在哪里添加这个
  • 好吧,这很好,我将带有完整 url 的 标签放在头部,瞧,它完成了。谢谢接受。
【解决方案2】:

试试这个

  RewriteRule ^interact/profile1.php/(.*)$ interact/profile1.php?id=$1 [L]

我不知道 QSA 修饰符是做什么的,所以我将其删除。如果您知道什么,请使用它)

【讨论】:

  • 不起作用。我认为在引用 profile1.php 文件的 php 文件中可能还需要进行一些更改。
  • @hritikwatts 这有点奇怪,因为它对我很有用。诀窍是左侧部分是您想在 url 中向用户显示,右侧部分是您将向服务器部分显示。在你的问题 RewriteRule 是错误的,或者我不明白你想要做什么。
  • 那么如何发送对变量的引用以将其显示为 interact/profile/34 ,因为对我来说我已经显示了上面的代码并且 profile1.php 使用 isset 接受变量。我是什么做错了吗?
猜你喜欢
  • 2015-07-24
  • 2011-04-28
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
  • 2012-07-17
  • 2018-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多