【问题标题】:Frustrating .htaccess URL Rewriting issue令人沮丧的 .htaccess URL 重写问题
【发布时间】:2012-04-03 02:17:37
【问题描述】:

我正在与一位客户合作,我们最近将他们的网站从 Godaddy (ew!) 迁移到了 Pair Networks。我个人和客户使用 Pair 已经很多年了,并且与他们有很好的经验。不幸的是这个问题,因为它是一个共享托管计划 URL 重写技术支持不属于支持计划。

无论如何,一旦我们将站点移至根目录中的重写规则,.htaccess 就不再起作用了。 .htaccess 包含:

RewriteEngine On
RewriteRule ^((images|styles|scripts|documents)/*.*)$ $1 [L]
RewriteRule ^(admin/*)$ index.php/$1 [L]
RewriteRule ^([a-zA-Z0-9]+)$ page/id/$1
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]

根据 Apache 的错误是重定向过多,导致显示错误 500。如果我注释掉这个:

#RewriteRule ^(.*)$ index.php/$1 [L]

然后加载默认页面(如果没有在 URL 中指定,则 index.php 设置默认值),但是其他页面都无法加载。我不得不手动将每种 URL 格式的条目添加到 .htaccess 以保持网站正常运行,结果如下:

RewriteEngine On
RewriteRule ^((images|styles|scripts|documents)/*.*)$ $1 [L]    
RewriteRule ^(admin/*)$ index.php/$1 [L]
RewriteRule ^(admin/([a-zA-Z0-9]+))$ index.php/$1 [L]
RewriteRule ^(admin/([a-zA-Z0-9]+)/([a-zA-Z0-9]+))$ index.php/$1 [L]
RewriteRule ^(page/id/*)$ index.php/$1 [L]
RewriteRule ^(page/submit)$ index.php/page/submit [L]
RewriteRule ^([a-zA-Z0-9]+)$ index.php/page/id/$1
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
#RewriteRule ^(.*)$ index.php/$1 [L]

该站点使用 Codeigniter,因此预期的最终重写 URL 格式为:http://server/index.php/page/id/example 以加载 http://server/example

我在 Pair 服务器上有另一个客户端使用完全相同的 CMS 和相同的原始 .htaccess 大约 6 年没有问题,所以我有点惊讶为什么这会突然导致问题。他们甚至都使用相同版本的 Apache 和 PHP(分别为 2.2.22 和 5.3.8)。

有人对问题可能是什么有想法吗?我宁愿不必像这样手动定义所有路线。

【问题讨论】:

    标签: php codeigniter url-rewriting


    【解决方案1】:

    多年来,我们一直使用一套非常相似的规则,没有任何问题。除了我们在该行前面有两个 RewriteCond 规则来检查请求不是针对文件或目录:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    

    【讨论】:

    • 哇,这就是它所需要的!不敢相信我以前没有尝试过,我想我被这个问题所困扰,为什么它可以在一台服务器上工作,而不是在新的服务器上工作,甚至没有想到这一点。感谢您的建议!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 2011-05-08
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 2012-12-17
    相关资源
    最近更新 更多