CodeIgniter查询字符串重写在.htaccess中

时间:2021-08-03 10:51:46

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

当我第一次启动我的网站时,url的格式如下:

project.php?projectID=1&pageID=2

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

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

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.

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

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。

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

我尝试在CodeIgniter中本地支持查询字符串,这似乎破坏了分段url,因此看起来我不能同时支持这两个。在CodeIgniter中设置一条路由,将查询字符串重定向到控制器,这似乎行不通。例如:

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

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

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

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

(注意,我还使用了.htaccess重写,它消除了索引的外观。在CI URL中的php)

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

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.

然而,重写规则似乎也不起作用。它重新定向到我的CodeIgniter 404,无需重写,甚至删除默认索引。php规则它不会重定向,所以我假设regex/中有一些内容。这里的htaccess格式不正确。

1 个解决方案

#1


3  

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

您需要匹配请求,而不是URI。URI中不会包含任何查询字符串:

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]

#1


3  

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

您需要匹配请求,而不是URI。URI中不会包含任何查询字符串:

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]