1

我正在尝试转换查询字符串;

http://atwd/books/course?course_id=CC100&format=XML&submit=Submit

成一个段URI;

http://atwd/books/course/CC100/XML

我在 CodeIgniter 工作。

我正在查看一个 stackoverflow 答案,该答案说要检查 CodeIgniter 的 URL 段指南,但我认为没有任何关于如何将查询字符串转换为段 URI 的信息。但是,有一种方法可以将段 URI 转换为查询字符串,这也从 Google 带来了大量结果。

另一个 stackoverflow 回答之后,我在我的 .htaccess 文件中尝试了这个,但似乎没有任何效果

RewriteCond %{QUERY_STRING} ^course_id\=([^&]+)\&format\=([^&]+)$
RewriteRule ^$ /course/%1/format/%2 [R,L]

在我的整个 .htaccess 文件中,我有这个;

<IfModule mod_rewrite.c>
RewriteEngine on

#Source: http://codeigniter.com/user_guide/general/urls.html
#Removal of index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?route/$1 [L]

#Source: https://stackoverflow.com/questions/3420204/htaccess-get-url-to-uri-segments
#Format Course function requests
RewriteBase /
RewriteCond %{QUERY_STRING} ^course_id\=([^&]+)\&format\=([^&]+)$
RewriteRule ^$ /course/%1/format/%2 [R,L]
</IfModule>

这是在我的 Codeigniter屏幕截图的根目录中

我在 .htaccess 文件中的代码不起作用,我刷新页面并没有任何反应。隐藏 index.php 的代码虽然有效。有谁知道为什么?

4

3 回答 3

1

从一件事到另一件事的“转换 URL”的概念是完全模棱两可的,请参阅此答案的顶部以了解重定向或重写时 URL 会发生什么:https ://stackoverflow.com/a/11711948/851273

发生了两件事,我会大胆尝试并猜测您想要第二件事,因为您抱怨刷新页面没有任何作用。


当您http://atwd/books/course?course_id=CC100&format=XML&submit=Submit在浏览器中键入时,这是通过 mod_rewrite: 发送的请求 URI /books/course。在您的规则中,您正在匹配一个空白 URI: RewriteRule ^$ /course/%1/format/%2 [R,L]。这是您的规则不起作用的第一个原因。它不起作用的第二个原因是因为除此之外,除了图像和 index.php 和 robots.txt 之外的所有内容都通过 index.php 进行路由。因此,即使您匹配正确URI,它也会在您的规则执行任何操作之前被路由。

您需要更正规则中的模式以匹配您希望重定向的 URI,并且您需要将此规则放在您拥有的路由规则之前。所以一切都应该大致是这样的:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteBase /
RewriteCond %{QUERY_STRING} ^course_id\=([^&]+)\&format\=([^&]+)$
RewriteRule ^/?books/course$ /course/%1/format/%2 [R,L]

#Source: http://codeigniter.com/user_guide/general/urls.html
#Removal of index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?route/$1 [L]

</IfModule>

您需要调整路径以确保它们与您实际查找的内容相匹配。


要重定向浏览器并在内部重写您的原始 URL,您需要做一些不同的事情。

首先,您需要确保所有链接看起来像这样:/course/CC100/format/XML. 更改您的 CMS 或静态 HTML,以便所有链接都以这种方式显示。

然后,您需要更改周围的规则(都您的 codeigniter 路由规则之前),如下所示:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteBase /

# redirect browser to a URI without the query string
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /books/course/?\?course_id=([^&]+)&format=([^&]+)
RewriteRule ^/?books/course$ /course/%2/format/%3? [R,L]

# internally rewrite query string-less request back to one with query strings
RewriteRule ^/?course/([^/]+)/format/([^/]+)$ /books/course?course_id=$1&format=$2&submit=Submit [L]

#Source: http://codeigniter.com/user_guide/general/urls.html
#Removal of index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?route/$1 [L]

</IfModule>
于 2013-01-14T16:28:27.147 回答
0

如果我对你的理解正确,你可能想多了。我自己的代码中有类似的东西。

我有一个名为 Source 的控制器。在那个控制器中,我有以下方法:

public function edit($source_id, $year)
{
    # Code relevant to this method here
}

这会产生:http://localhost/source/edit/12/2013,其中 12 指的是$source_id2013 指的是$year。您添加的每个参数都会自动转换为它自己的 URI 段。它也不需要 .htaccess 诡计或自定义路由。

于 2013-01-15T01:16:32.467 回答
0

我不打算解决已经在其他答案和评论中很好地解决的误解,我不能专门代表 CodeIgniter,但是快速浏览了他们的URL 路由文档,它似乎与大多数 Web 框架非常相似:


您可能只想将所有流量(与物理文件不匹配)引导到前端 Web 控制器(index.php)并在 CodeIgniter 的路由中处理 URL 管理,而不是 htaccess 文件。

为此,您的 htaccess 可以简单到:

<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule .* index.php [QSA,L]
</IfModule>

正如我所说,这会将任何与物理文件(例如 robots.txt 或图像)不匹配的流量重定向到您的 index.php。

然后,使用文档( http://ellislab.com/codeigniter/user-guide/general/routing.html )中描述的路由,您可以接收参数并将它们传递给您认为合适的控制器,没有需要“转换”或“映射”任何内容,您的 URL 不需要在/?yada=yada内部解析,根据您的路由规则 CodeIgniter 可以解决。

您需要来自文档的通配符路由,例如:

$route['product/:id'] = "catalog/product_lookup";

您最终可能看起来像的粗略示例如下:

$route['course/:id/format/:format'] = "course/something_or_other_action";
于 2013-01-14T20:59:38.073 回答