我正在尝试转换查询字符串;
http://atwd/books/course?course_id=CC100&format=XML&submit=Submit转换为段URI;
http://atwd/books/course/CC100/XML我在CodeIgniter工作。
我看到了一个堆栈溢出的答案,上面说要检查CodeIgniter的URL段指南,但我不认为有任何关于如何将查询字符串转换为段URI的信息。然而,有一种方法可以将片段URI转换为查询字符串,这也带来了来自Google的大量结果。
在another stackoverflow answer之后,我在我的.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 screenshot根目录中
我在.htaccess文件中的代码不工作,我刷新了页面,但没有任何反应。不过,隐藏index.php的代码正在运行。有人知道为什么吗?
发布于 2013-01-15 00:28:27
将URL从一件事转换为另一件事的概念是完全模糊的,请参阅此答案的上半部分,以了解在重定向或重写时URL会发生什么:https://stackoverflow.com/a/11711948/851273
有两件事会发生,我将大胆尝试一下,猜想你想要第二件事,因为你抱怨刷新页面没有任何作用。
在您的规则中,您将匹配一个空白URI:RewriteRule ^$ /course/%1/format/%2 [R,L]。这是你的规则不起作用的第一个原因。
您需要更正规则中的模式以匹配您希望重定向的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>首先,你需要确保你所有的链接看起来像这样:/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-15 04:59:38
我不打算解决已经在其他答案和评论中很好地解决的误解,我也不能特别为CodeIgniter说话,但在快速浏览了他们的URL routing docs之后,它看起来与大多数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),你可以接受参数并将它们传递给你的控制器,在你认为合适的时候,没有必要‘转换’或‘映射’任何东西,你的网址不需要在内部解析为/?yada=yada,基于你的路由规则,CodeIgniter可以解决它。
您将需要来自文档的如下通配符路由:
$route['product/:id'] = "catalog/product_lookup";下面是一个粗略的示例,说明您的应用程序最终可能会是什么样子:
$route['course/:id/format/:format'] = "course/something_or_other_action";发布于 2013-01-15 09:16:32
如果我没理解错的话,你可能想得太多了。我在自己的代码中也有类似的东西。
我有一个名为Source的控制器。在该控制器中,我有以下方法:
public function edit($source_id, $year)
{
# Code relevant to this method here
}这会产生:http://localhost/source/edit/12/2013,其中12表示$source_id,2013表示$year。您添加的每个参数都会自动转换为其自己的URI段。它也不需要.htaccess技巧或自定义路由。
https://stackoverflow.com/questions/14317440
复制相似问题