【问题标题】:.htaccess rewrite not doing anything.htaccess 重写什么都不做
【发布时间】:2012-10-30 20:34:14
【问题描述】:

我有一个 URL 为 http://www.freshmarketstores.com/departments/produce/recipe.php?ing=Akane_Apples&recipe=59 的页面,我希望将其清理为类似于 http://www.freshmarketstores.com/departments/produce/recipe/Akane_Apples/59 的页面

现在,在位于生产文件夹中的 .htaccess 文件中,我有以下代码:

RewriteEngine on
RewriteRule ^recipe/(.*)/(.*)$ recipe.php?ing=$1&recipe=$2 [L]

目前,这不会影响任何事情。当recipe.php?ing=Akane_Apples$recipe=59 url 被加载时,它加载得很好,但不进行重写。这是我第一次尝试 URL 重写,但无法解决这个问题。

提前感谢您的帮助。

编辑: 好的,所以经过昨晚的一些学习后,我意识到我正在倒退。我可以输入http://www.freshmarketstores.com/departments/produce/recipe/Akane_Apples/59 并获取存储在.../produce/recipe.php?ing=Akane_Apples&recipe=59 的资源。感谢您为此提供的所有帮助。

我现在的问题是,你能走相反的路吗?如果我输入 .../produce/recipe.php?ing=Akane_Apples&recipe=59,我怎样才能让它重定向到 /produce/recipe/Akane_Apples/59?

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite rewrite


    【解决方案1】:

    确保你有

    AccessFileName .htaccess
    

    在您的服务器和 apache 中的虚拟主机配置文件中设置,默认设置为 .htaccess,但它不仅限于此,可以命名任何内容。

    基于 debian 的系统的默认配置文件是 /etc/apache2/apache2.conf,而对于大多数其他系统,默认配置文件是 apache 安装目录中的 httpd.conf

    一旦设置了访问文件并启用了 mod_rewrite,您就可以执行以下操作

    http://www.freshmarketstores.com/departments/produce/Akane_Apples/59

    RewriteEngine On
    RewriteRule ^recipe/([^/]*)/([^/]*)$ /recipe.php?ing=$1&recipe=$2 [L]
    

    【讨论】:

    • 如果我在 Bluehost 上会有什么影响。我不相信我可以访问 httpd.conf 文件。我也知道 .htaccess 文件本身在工作,因为我做了一个测试: RewriteEngine on RewriteRule ^test\.html$ test.php [L] 将 test.html 重定向到 test.php,效果很好。
    • 感谢您的帮助,但这仍然无法正常工作。为了让您知道 mod_rewrite 正在工作,我已更改 .htaccess 文件以将以下链接 freshmarketstores.com/departments/produce/test.html(应该只显示“这是 html 页面”)改写为 freshmarketstores.com/departments/produce/test.php,其中显示“这是php页面”。所以我知道 Rewrite 正在工作,但好像配方语句的 Regex 部分不匹配
    • 任何示例都有效吗?我的还是其他帖子?也许您需要遵循教程才能使其正常工作link 这也将使您对 htaccess 有一个更好和更深入的理解
    • 所有示例都不起作用,我从教程开始了整个过程。我完全同意我需要对这一切有更好、更深入的理解。我现在正在看你的链接。谢谢!
    【解决方案2】:

    通过httpd.conf启用mod_rewrite和.htaccess,然后把这段代码放到你.htaccess的DOCUMENT_ROOT目录下:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    # internal forwarding to /recipe.php
    RewriteRule ^recipe/([^/]+)/([^/]+)/?$ recipe.php?ing=$1&recipe=$2 [L,QSA,NC]
    
    # external redirect to /recipe
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+recipe\.php\?ing=([^&]+)&recipe=([^\s]+) [NC]
    RewriteRule ^ recipe/%1/%2? [R=301,L]
    

    【讨论】:

    • 感谢您提供帮助,但这不起作用。它仍然只是正常加载。
    • .htaccess 和 mod_rewrite 是否启用?您正在测试的 URI 是什么?
    • 我对所有这些 .htaccess 和 mod_rewrite 东西都非常陌生,但我会说它们肯定是启用的(我会这样说是因为如果你访问 freshmarketstores.com/departments/produce/test.html 它会重写freshmarketstores.com/departments/produce/test.php [由显示“这是 php 页面”的页面上的类型证明])。我在 Bluehost 上使用共享托管计划,所以我知道 mod_rewrite 已启用(并且我无权访问 .httpd.conf 文件)。我还应该看什么?
    • 要测试 .htaccess,你可以在上面放一些垃圾文本,看看是否会产生 500 错误。
    • 是的,当我将垃圾文本放在 .htacess 文件的顶部时出现 500 错误。
    【解决方案3】:

    你应该没问题

    RewriteEngine on
    RewriteCond %{THE_REQUEST} \?ing=([^&]+)&recipe=([^&]+) [NC]
    RewriteRule ^(departments/produce/recipe)\.php$ http://%{HTTP_HOST}/$1/%1/%2 [NC,R=301,L]
    

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 2018-08-19
      • 2014-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      • 2019-08-16
      相关资源
      最近更新 更多