【问题标题】:Legacy URLs in CakePHP using routes or htaccessCakePHP 中使用路由或 htaccess 的旧版 URL
【发布时间】:2011-03-17 18:08:29
【问题描述】:

我正在将网站从“普通的旧”php 迁移到 CakePHP 框架,我需要能够处理一些遗留 URL,例如:

foo.com/bar.php?id=21

是当前URL的模式,但是在使用cake的新站点中,该URL需要重定向到:

foo.com/blah/21

所以基本上,我需要能够获取该 ID 号并将其传递。

可能是因为我睡了大约 4 个小时,但无论我得到什么正则表达式模式,我似乎都无法让它在 /app/webroot/.htaccess 中工作。

我对任何使用 .htaccess 或 routes.php 的解决方案持开放态度 - 无论哪种方式,我都将不胜感激!

【问题讨论】:

  • 你能把你的 .htaccess 结果贴出来吗?

标签: regex url .htaccess cakephp routes


【解决方案1】:

稍作调整,最终的改写结果如下:

RewriteEngine On
# My hack to get Legacy URLs to work
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^bar\.php$ http://foo.com/bar/%1? [R=301,L]
# Cake's Default Rewrite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

注意%1 后面的? - 没有它,重写是foo.com/bar/21?id=21,有它,它是foo.com/bar/21

【讨论】:

    【解决方案2】:

    我想你可能会在这里找到解决方案:http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/,使用类似的东西:

    RedirectMatch ^/oldfolder/(.*)$ http://mydomain.site/newfolder/$1
    

    可能很难让您了解 htaccess - 我的脑海中没有太多空间 - 但值得坚持使用 apache 手册并查看 Google 提供的数十万个教程和解决方案中的一些会回来的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2021-01-05
      • 2016-01-09
      相关资源
      最近更新 更多