.htaccess中的CodeIgniter查询字符串重写 [英] CodeIgniter Query String Rewrite in .htaccess

查看:79
本文介绍了.htaccess中的CodeIgniter查询字符串重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我首次启动网站时,URL的格式如下:

When I first launched my site, URLs were in the following format:

project.php?projectID=1&pageID=2

几年前,我修改了.htaccess,将其重写为使用段,如下所示:

A few years ago I modified my .htaccess to rewrite these to use segments, like this:

project/1/2

我更新了所有内部链接以使用分段格式,但这很好,因为它仍然支持旧格式的所有外部传入链接.

I updated all internal links to use the segmented format, but this was good because it still supported any external incoming links in the old format.

现在,我已切换到CodeIgniter,使用分段格式呈现页面很容易,但是我正在努力研究如何支持旧的查询字符串URL.

Now I have switched to CodeIgniter, having my pages render using the segmented format is easy, but I'm struggling to work out how to support the old query-string URLs.

我尝试在CodeIgniter中本机支持查询字符串似乎破坏了分段的URL,因此看来我不能同时支持两者.在CodeIgniter中设置路由以将查询字符串重定向到控制器似乎不起作用.例如:

My attempts to support query strings natively in CodeIgniter seem to break the segmented URLs, so it looks like I cannot have both supported simultaneously. Setting up a route in CodeIgniter to redirect a query string to a controller doesn't seem to work. E.g.:

$route['project.php?projectID=(:any)&pageID=(:any)'] = 'home/projectview/$1/$2';

我的第二个想法是直接修改.htaccess,以将查询字符串重定向到控制器,如下所示:

My second thought was to modify the .htaccess directly to redirect the query string to the controller, like so:

(请注意,我还使用了.htaccess重写,从而消除了CI URL中index.php的出现)

(Note I am also using the .htaccess rewrite that eliminates the appearance of index.php in the CI URL)

RewriteRule ^project.php?projectID=(\d*)&pageID=(\d*) project/$1/$2/ [L]
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]

但是,此重写规则似乎也不起作用.它无需重新编写即可重定向到我的CodeIgniter 404,甚至通过删除默认的index.php规则也不会重定向,因此我假设regex/.htaccess格式中的某些内容不正确.

However this rewrite rule doesn't appear to work either. It redirects to my CodeIgniter 404 without being rewritten, and even by removing the default index.php rule it doesn't redirect, so I'm assuming there is something in my regex/.htaccess formatting here that is incorrect.

推荐答案

您需要根据请求而不是URI进行匹配. URI中没有任何查询字符串:

You need to match against the request and not the URI. The URI won't have any of the query string in it:

RewriteCond %{THE_REQUEST} \ /+project\.php\?projectID=([0-9]+)(?:&pageID=([0-9]+)|)
RewriteRule ^ /project/%1/%2? [L,R=301]

RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]

这篇关于.htaccess中的CodeIgniter查询字符串重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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