You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter中.htaccess多参数传递及301重定向问题求助

Fixing 301 Redirects & Passing Multiple Params in CodeIgniter .htaccess

1. Fixing Your 301 Redirect Rule

Let's break down why your current rule isn't working first:

  • You’re using redirect (from Apache’s mod_alias module), which doesn’t support regex capture groups the way you’re trying to use them. You need RewriteRule (from mod_rewrite) instead—this is the same module CodeIgniter relies on for its core clean URL routing.
  • Your capture group numbering was completely off: you referenced $2 without defining any groups first, and the structure didn’t align with your old URL’s segment order.

Looking at your URL structure:

Old URL: /service-centers/Rajasthan/Kekri/Riico-Industrial-Area/Riico-Industrial-Area--Shoes-Factory/Ekam-Motors/370
New URL: /car-service-centers/Kekri/Ekam-Motors/370

We need to match all segments after /service-centers/, then keep only the 2nd, 5th, and 6th segments for the new URL. Here’s the corrected rule:

# Place this BEFORE your existing CodeIgniter rewrite rules
RewriteEngine On
RewriteRule ^service-centers/[^/]+/([^/]+)/[^/]+/[^/]+/([^/]+)/([^/]+)$ /car-service-centers/$1/$2/$3 [L,R=301]

Let’s break this down:

  • ^service-centers/ matches the start of the URL path
  • [^/]+ matches any URL segment (excluding slashes) — we use this for the segments we want to ignore (Rajasthan, Riico-Industrial-Area, Riico-Industrial-Area--Shoes-Factory)
  • ([^/]+) is a capture group for segments we need to retain:
    • $1 = Kekri (2nd segment after /service-centers/)
    • $2 = Ekam-Motors (5th segment)
    • $3 = 370 (6th segment)
  • [L,R=301] flags: L tells Apache this is the last rule to process if matched; R=301 sends a permanent 301 redirect signal to search engines and browsers.

Important notes:

  • Ensure RewriteEngine On is already present in your .htaccess (it should be part of CodeIgniter’s default setup)
  • Place this rule before the default CodeIgniter rule that routes all requests to index.php—otherwise, that rule will catch the request first and your redirect won’t trigger.

2. Passing Multiple Parameters in CodeIgniter .htaccess

CodeIgniter’s default .htaccess is already designed to pass all URL segments to the front controller (index.php), which then handles routing to your controllers and methods.

Default CodeIgniter .htaccess Setup

If you don’t have it already, here’s the standard rule to enable clean URLs and pass all parameters:

# Place this AFTER your redirect rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

How Parameters Work

When a request like yourdomain.com/controller/method/param1/param2/param3 comes in, the .htaccess rule passes the entire path (controller/method/param1/param2/param3) to index.php. CodeIgniter then parses this into:

  • Controller: controller
  • Method: method
  • Parameters: param1, param2, param3 (you can access these via $this->uri->segment(3), $this->uri->segment(4), etc., or directly as method arguments if your controller method accepts them)

Custom Routing for Specific Parameter Patterns

If you need to map unique URL patterns to controllers/methods with multiple params, it’s cleaner to use CodeIgniter’s routes.php (located in application/config/routes.php) instead of modifying .htaccess. For example:

// Map /service-centers/location/shop/id to ServiceCentersController->view($location, $shop, $id)
$route['service-centers/(:any)/(:any)/(:num)'] = 'ServiceCentersController/view/$1/$2/$3';

This keeps all routing logic within your application, rather than tying it to server configuration.


内容的提问来源于stack exchange,提问作者priya gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 06:52:41