CodeIgniter Forums
need help with .htaccess - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: need help with .htaccess (/showthread.php?tid=53227)



need help with .htaccess - El Forum - 07-16-2012

[eluser]Yours3lf[/eluser]
Hi,

I wrote a CI app, and now it's time to make it work. The app is divided to controllers that have functions. Now if I want to access a controller's function I have to do this:
http://example.com/controller/function1/param1/param2/param3/param4
Now to make this google friendly I tried to write a .htaccess file, which rewrites the urls so that each function has a specific name that is translated. For example:
http://example.com/name1/param1/param2/param3/param4
translates to:
http://example.com/controller/function1/param1/param2/param3/param4

but this wouldn't work. I tried to debug it by redirecting the url (using [R]), and that way it did work, but without that it doesn't.

Here's the .htaccess code:
Code:
RewriteEngine on
RewriteBase /

#if the url starts with /name1/...
RewriteCond $1 ^(name1)
#then replace it with /controller/function1/...
RewriteRule ^(name1)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ controller/function1

#if the url doesn't contain index.php, css, design, pics, robots.txt, license.txt at the beginning
RewriteCond $1 !^(index\.php|css|design|pics|robots\.txt|license\.txt)
#then precede the url (containing any character) with /index.php/
#this will be the last transform, as previously we made sure that the nice urls are understood
#RewriteRule ^(.*)$ /index.php/$1 [R,L]
RewriteRule ^(.*)$ /index.php/$1 [L]

what am I doing wrong?


need help with .htaccess - El Forum - 07-16-2012

[eluser]Adam Liszkai[/eluser]
Hi!

My .htacess file:

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php?$1 [L]

And you can rename in /application/config/routes.php

Code:
$route['default_controller'] = "page";
$route['404_override'] = '';

$route['^(?!admin|login|download).*'] = "page/lang/$0";

Here when the url fisrt section is not admin or login or download then rewrite the code page/lang/ parameters...

And if you want it rename this shuld work(but im not test it):

Code:
$route['^(?name1).*'] = "name1/function/$0";

Cheers! Wink


need help with .htaccess - El Forum - 07-16-2012

[eluser]Yours3lf[/eluser]
Thanks Adam Liszkai,

I didn't know CI has a feature like this Smile
This gave me the idea to do things like this.
The suggested form of the regex didn't work, but after looking up this feature in the CI user guide, I came up with this:

$route['name1/(:any)'] = "controller/function1/$1";

works like charm Big Grin


need help with .htaccess - El Forum - 07-16-2012

[eluser]Adam Liszkai[/eluser]
Smile your wellcome