Welcome Guest, Not a member yet? Register   Sign In
mod_rewrite and CodeIgniter question
#1

[eluser]ChrisF79[/eluser]
I'm new to CodeIgniter and I'm about to redesign a real estate site. Right now a URL is something like http://www.mydomain.com/Naples-Real-Estate/20384729/ where the 20384729 is the MLS Number for the database. I use explode to get that value with plain PHP. With CodeIgniter, would I still be able to use mod_rewrite to have that access a property.php sort of page?

Thank you
#2

[eluser]WanWizard[/eluser]
With CI you need mod_rewrite to get rid of the index.php in the URL. In general you don't need it to rewrite URL's.

Assuming your property.php is (going to be) a controller, you can use a route to convert http://www.mydomain.com/Naples-Real-Estate/20384729/ to http://www.mydomain.com/property/index/N.../20384729/.

You can then define your index method as
Code:
function index($name='', $MLS='')
{
}
where name will be 'Naples-Real-Estate' and MLS the mls number.

If you're not interested in the name, you can also use routes to remove that from the URI passed to the controller.
#3

[eluser]ChrisF79[/eluser]
But then does the user see that long URL or the original one I posted?
#4

[eluser]WanWizard[/eluser]
Routing is something that happens internally, the URI the user typed in doesn't change.
#5

[eluser]ChrisF79[/eluser]
What would the route look like on something like that?
#6

[eluser]WanWizard[/eluser]
Something like
Code:
// route all combinations of alphanumeric/numeric:
$route['(:any)/(:num)'] = 'property/index/$1/$2';

// route Naples-Real-Estate to the naples method
$route['Naples-Real-Estate/(:num)'] = 'property/naples/$1';

You can even use a regex to match the weirdest combination of characters in the URI. Read up on URI Routing in the user guide.
#7

[eluser]ChrisF79[/eluser]
That's super helpful. Thanks so much for all the replies. I'm headed off to try now. Thanks again!
#8

[eluser]ChrisF79[/eluser]
[quote author="WanWizard" date="1284335754"]Something like
Code:
// route all combinations of alphanumeric/numeric:
$route['(:any)/(:num)'] = 'property/index/$1/$2';

// route Naples-Real-Estate to the naples method
$route['Naples-Real-Estate/(:num)'] = 'property/naples/$1';
[/quote]
I tried adding your suggestion to my routes.php file. I have the url helper autoloaded in the autoload file. However, when I then try to visit http://localhost:8888/ci/Naples-Real-Estate/20292846/ I'm getting a not found error. It says:
The requested URL /ci/Naples-Real-Estate/20292846/ was not found on this server.
#9

[eluser]danmontgomery[/eluser]
Do you have mod_rewrite enabled and an .htaccess to remove index.php from your URL? Does http://localhost:8888/ci/index.php/Naple.../20292846/ work?
#10

[eluser]ChrisF79[/eluser]
[quote author="noctrum" date="1284425526"]Do you have mod_rewrite enabled and an .htaccess to remove index.php from your URL? Does http://localhost:8888/ci/index.php/Naple.../20292846/ work?[/quote]
So close! If I have in my routes.php file, $route['Naples-Real-Estate/(:num)'] = "property/naples/$1"; I get a CodeIgniter 404. If I take out the /naples/$1 part, I get a blank page. The weird thing is, my view just says in HTML "Your view has been loaded." I'm not even seeing that text. I did the mod_rewrite to remove the index.php so I think its loading the view but I have no way of knowing.




Theme © iAndrew 2016 - Forum software by © MyBB