Welcome Guest, Not a member yet? Register   Sign In
htaccess / routing CI2 + EasyPHP
#1

[eluser]Unknown[/eluser]
Hi All
I have a problem with htaccess / routing and it's doing my head in now. I've been programming in PHP for 10+ years, CI for 2+ years, have used htaccess for many years for URL rewriting etc.

I am reprogramming an old PHP website into CI. Most of it is working fine. I'm running EasyPHP on my laptop. Many CI websites work fine on this.
One controller needs to use the old query strings - this is the shopping cart. The new controller receives the GET variables just fine. However, rewriting the old URLs to the new controller isn't working.

The basic controller code is:
Quote:class Mycart extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->get_data = array();
parse_str($_SERVER['QUERY_STRING'],$this->get_data);
$this->get_data = $this->security->xss_clean($this->get_data);
}

public function index()
{
print_r($this->get_data);
}
}

The new URL to access this on my local dev server is:
http://127.0.0.1/dev/mycart/?alpha=123&beta=234

That all works fine

The old URLs on the old website used a cart URL like this:
http://127.0.0.1/dev/cart/index.php?alpha=123&beta=234

In theory, it should be a simple to replace 'cart/index.php' with 'mycart/' in htaccess, but it just won't work

This is part of my htaccess:
Quote:<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
#the following line is the one I'm having trouble with:
RewriteRule ^cart/index\.php$ http://127.0.0.1/dev/mycart [L]
#everything below this works fine
RewriteRule ^index\.html$ http://127.0.0.1/dev [L]

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /dev/index.php?/$1 [L]

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /dev/index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dev/index.php?/$1 [L]

</IfModule>

Please focus only on that one line.
I have tried every combination of values left and right that I can think of, with and without leading slashes, absolute URLs etc - for example:
RewriteRule ^cart/index\.php$ mycart [L]
RewriteRule ^/cart/index\.php$ /mycart [L]
RewriteRule ^dev/cart/index\.php$ dev/mycart [L]
RewriteRule ^/dev/cart/index\.php$ /dev/mycart [L]
RewriteRule ^/dev/cart/index\.php$ mycart [L]
RewriteRule ^/dev/cart/index\.php$ /mycart [L]
RewriteRule ^/dev/cart/index\.php$ dev/mycart [L]
RewriteRule ^/dev/cart/index\.php$ /dev/mycart [L]
RewriteRule ^/dev/cart/index\.php$ http://127.0.0.1/dev/mycart [L]
(I've tried dozens and dozens of variations like these)
I've also used (.*) and $1 in the correct places - no effect

I've tried using RewriteCond %{REQUEST_URI} ^cart [NC] and variations of the RewriteCond - none of it works

My apache (EasyPHP) log files report:
/dev/cart/index.php?alpha=123&beta=234 HTTP/1.1 404
They are always the same (old) URL as typed into the browser

Interestingly the following line 'works' and redirects to google:
RewriteRule ^cart/index\.php$ http://google.com [L]
However, the following line doesn't work at all:
RewriteRule ^cart/index\.php$ http://127.0.0.1/dev/mycart [L]

Any thoughts?

I'm also using APP/core/MY_Router.php which has the following:
function _set_request ($seg = array())
{
// The str_replace() below goes through all our segments
// and replaces the hyphens with underscores making it
// possible to use hyphens in controllers, folder names and
// function names
parent::_set_request(str_replace('-', '_', $seg));
}
This works fine too


#2

[eluser]jonez[/eluser]
You should do rewrites in the router, this may be helpful: http://ellislab.com/codeigniter/user-gui...uting.html

If you're trying to pass parameters to index() methods you'll need to use remap.

Code:
public function __construct( ) {
  parent::__construct( );
}

public function _remap( $method, $params = array( ) ) {
  if ( method_exists( $this, $method ) ) {
   return call_user_func_array( array( $this, $method ), $params );
  }
  else {
   array_unshift( $params, $method );
   return call_user_func_array( array( $this, 'index' ), $params );
  }
}

public function index( $encoded_id, $user = null ) {

...




Theme © iAndrew 2016 - Forum software by © MyBB