如何转换查询字符串段URI? [英] How to Convert Query String to Segment URI?

查看:237
本文介绍了如何转换查询字符串段URI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个查询字符串;

I'm trying to convert a query string;

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

到段URI;

Into a segment URI;

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

我的工作在codeIgniter。

I'm working in CodeIgniter.

我一直在寻找一个计算器的答案,上述检查codeIgniter的URL段指导,但我不认为有关于如何查询字符串转换成段的URI的任何信息。然而,有一种方法,以一个段的URI转换成一个查询串,这是造就结果的负载从谷歌太。

I was looking at a stackoverflow answer that said to check CodeIgniter's URL segment guide, but I don't think there's any information on how to convert a query string into a segment URI. There is, however a way to convert a segment URI into a query string, which is bringing up a load of results from Google too.

另一个计算器的答案,我在我的.htaccess文件中尝试这样做但没有什么似乎工作

Following another stackoverflow answer, I tried this in my .htaccess file but nothing seemed to work

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

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

In my entire .htaccess file I have this;

<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: http://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 截图

This is in my root directory of Codeigniter screenshot

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

My code in the .htaccess file isn't working, I refresh the page and nothing happens. The code to hide the index.php is working though. Does anyone know why?

推荐答案

转换网址从一件事的概念到另一个完全是不明确的,看到这个答案的上半部分为发生的网址是什么解释重定向时或改写: http://stackoverflow.com/a/11711948/851273

The notion of "converting URLs" from one thing to another is completely ambiguous, see the top part of this answer for an explanation of what happens to URLs when redirecting or rewriting: http://stackoverflow.com/a/11711948/851273

还有2的事情发生了,我要带野生刺和猜测,你想要的第二件事,因为你抱怨刷新页面没有做任何事情。

There's 2 things that happen, and I'm going to take a wild stab and guess that you want the 2nd thing, since you're complaining that refreshing the page doesn't do anything.

当你键入的http:// atwd /书籍/课程COURSE_ID = CC100和放大器;格式= XML和放大器;提交=提交到您的浏览器,这是请求的URI是什么?被通过的mod_rewrite发送: /书籍/课程。在您的规则,你匹配扫荡一空URI:重写规则^ $ /场/ 1%/格式/%2 [R,L] 。这是第一个你的规则不起作用的原因。为什么它不工作的第二个原因是因为上面说,一切,除了图像和的index.php的robots.txt是通过index.php文件被路由。所以,即使你的的反对权URI匹配,它就会被你的规则之前,路由甚至会做任何事情。

When you type http://atwd/books/course?course_id=CC100&format=XML&submit=Submit into your browser, this is the request URI that gets sent through mod_rewrite: /books/course. In your rule, you are matching against a blank URI: RewriteRule ^$ /course/%1/format/%2 [R,L]. That's the first reason your rule doesn't work. The second reason why it doesn't work is because above that, everything except images and index.php and robots.txt is being routed through index.php. So even if you were matching against the right URI, it gets routed before your rule even gets to do anything.

您需要更正您的规则匹配您希望重定向的URI模式,你需要把这个规则的的,你有路由规则。所以,一切都应该大致如下:

You need to correct the pattern in your rule to match the URI that you expect to redirect, and you need to place this rule before the routing rule that you have. So everything should look roughly like this:

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

您将需要调整的路径,以确保它们符合您实际想要的。

You'll need to tweak the paths to make sure they match what you are actually looking for.

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

To both redirect the browser and internally rewrite back to your original URL, you need to do something different.

首先,你需要确保所有的链接看起来像这样: /场/ CC100 /格式/ XML 。改变你的CMS或静态HTML,这样所有的链接显示的方式。

First, you need to make sure all of your links look like this: /course/CC100/format/XML. Change your CMS or static HTML so all the links show up that way.

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

Then, you need to change the rules around (all before your codeigniter routing rule) to be something liek this:

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

这篇关于如何转换查询字符串段URI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆