itwasthewind
itwasthewind

Reputation: 355

Mod Rewrite Newbie in CodeIgniter

I'm pretty new to mod-rewrites, and I'm having trouble getting this right:

I'd like http://myurl.com/special and http://www.myurl.com/special to both redirect to http://myurl/index.php/special without changing the url for the visitor in their browser.

currently, my .htaccess looks like this, to remove www from URLs

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.myurl.com [NC]
RewriteRule ^(.*)$ http://myurl.com/$1 [L,R=301]
</IfModule>

I tried a bunch of different things to redirect the special URL, but nothing has worked for me so far, each with a different undesirable result. Please let me know if you have an idea about what I can add here!

I tried this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^special$ /index.php/special$1 [NC]
RewriteCond %{HTTP_HOST} ^www.myurl.com [NC]
RewriteRule ^(.*)$ http://myurl.com/$1 [R=301]
</IfModule>

That makes http://myurl.com/special redirect to the default controller, and makes http://www.myurl.com/special redirect to http://myurl.com/index.php/special AND changes the url in the browser. Neither of those is quite right, although the second is closer to what I need.

Upvotes: 1

Views: 395

Answers (2)

BrandonS
BrandonS

Reputation: 932

oh no you don't need htaccess to do that just use CI's routes. Here first change your HTACESS to this:

# Customized error messages.
ErrorDocument 404 /index.php

# Set the default handler.
DirectoryIndex index.php

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>

This one is way way better and you never have to change it for anything not for img folders or anything just leave it pure. now for your issue.

open routes php in the Application->config folder.

Now in CI all files route through index.php by default so i am assuming you mean your default controller anyway to route "controllers" like in your case special back to the main site just do this:

if your default controller is still "welcome" as it is downloaded you would write a route like this:

$route['special/(:any)'] = "welcome";

your url will not change and everything will be perfect

BUT DONT STOP READING YET, JUST TO MAKE SURE.

this is what your routes would look like after your done:

//these two are for CI 
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";

//this is your route sending all and any traffic for special
//no matter how many levels it will always go to welcome->index.
$route['special/(:any)'] = "welcome";

//if you want to use sub-pages or other functions within your welcome controller 
//i.e. welcome->signup. then just send the route information to the controller like this:
$route['special/(:any)'] = "welcome/$1";

The routing class in php is very powerful and this is just a piece here for more detailed info on routing see this page:

CodeIgniter User Guide -> URI Routing

Really hope i haven't confused you more. Good Luck

Upvotes: 2

Peter O&#39;Callaghan
Peter O&#39;Callaghan

Reputation: 6186

Assuming www.myurl.com and myurl.com are both set-up as ServerAliases in your VirutalHost, then something as simple as...

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Upvotes: 1

Related Questions