【问题标题】:CakePHP and .htaccess: how to redirect url with query stringCakePHP 和 .htaccess:如何使用查询字符串重定向 url
【发布时间】:2010-10-28 22:34:20
【问题描述】:

我想将此网址从 /main.php?page=pages&id=gettingpaidfortakingsurveys 重定向到 /main/pages/getting-paid-for-taking-surveys。

谢谢!

[UDPATE]

我在现有的 cakephp .htaccess 上对其进行了测试,但没有成功。

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteRule ^/main.php?page=(.+)&id=(.+) /main/$1/$2 [NC]
 RewriteRule ^$ app/webroot/    [L]
 RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

【问题讨论】:

  • 对了,别忘了把 IfMaodule 改成 IfModule。

标签: .htaccess cakephp redirect


【解决方案1】:

由于您似乎想使用 Apache 的 .htaccess 来执行此操作,因此您需要 mod_rewrite 模块。在 .htaccess 中,你会想要这样的东西:

RewriteEngine on
RewriteRule ^main.php?page=(.+)&id=(.+) /main/$1/$2 [NC]

这应该让您/main/pages/gettingpaidfortakingsurveys。这不会使单词连字符,我不确定您是否能够在没有过于复杂的情况下做到这一点。

【讨论】:

  • 您好,看来代码不起作用。我更新了这篇文章,以便您可以看到新的 .htaccess。它实际上是在 cakephp
  • 您的 .htaccess 做得比您在问题中提出的要多,所以我无法帮助您,但您确实有一个错字IfMaodule。如果你做错了什么,请查看mod_rewrite 的文档,这只是基本的正则表达式。 httpd.apache.org/docs/1.3/mod/mod_rewrite.html
  • 我修正了那个错字。我需要修复它,所以我在 webroot 文件夹中创建了一个 main.php 并进行了一些重定向,它是临时的。感谢您的宝贵时间。
【解决方案2】:

你试过了吗,我假设这只是一个页面的重定向。您需要将它放在 Cake 重写块之上。第二部分必须是完整的 URL:

Redirect 301 /main.php?page=pages&id=gettingpaidfortakingsurveys http://www.domain.com/main/pages/getting-paid-for-taking-surveys
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^$ app/webroot/    [L]
    RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

如果重写是临时的,用 302 代替 301。

url 的 /main 部分看起来对 Cake 不太友好。不应该只是 /pages/...?

【讨论】:

    猜你喜欢
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 2022-03-24
    • 2017-03-06
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多