如何转换查询字符串段URI?(How to Convert Query String to Segm

2019-07-18 04:54发布

我想查询字符串转换;

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

成段URI;

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

我在笨工作。

我看到的是一个计算器回答说,检查CodeIgniter的URL段指导,但我不认为有关于如何查询字符串转换成段URI的任何信息的。 然而,有一种方法来段URI转换成一个查询字符串,它带来了结果的负载从谷歌了。

继另一计算器的答案 ,我想这在我的.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>

这是笨的我的根目录截图

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

Answer 1:

“将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>

你需要调整的路径,以确保它们符合您实际寻找。


这两个浏览器重定向和内部重写原来的网址,你需要做不同的事情。

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

然后,你需要改变周围的规则(所有CodeIgniter的路由规则之前 )要的东西liek这样的:

<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>


Answer 2:

我不打算解决误会已经解决得很好在对方的回答和评论,我不能讲笨特别,但已经给他们的URL路由文档快速脱脂,似乎很相似,大多数Web框架:


你可能只是想将所有流量(即不符合物理文件)的前端Web控制器(的index.php)和CodeIgniter的路由,而不是一个htaccess文件处理URL管理。

要做到这一点,你的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";


Answer 3:

如果我理解正确,你可能会过度思考它。 我有我自己的代码类似的东西。

我有一个名为源控制器。 在这种控制器,我有以下方法:

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弄虚作假或定制路由无论是。



文章来源: How to Convert Query String to Segment URI?