views:

47

answers:

2

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.

A: 

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>
Cags
+1  A: 

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

BrandonS
Hey Brandon, thanks for the quick response. The reason I had my .htaccess file like that was to have all requests to http://www.myurl.com redirect to http://myurl.com. What I need is for that to continue, but in addition to have all requests to http://myurl.com/special redirect to http://myurl.com/index.php/special.Any more help you can provide in doing that? I tried modifying the htaccess and routes files like you suggested, but that didn't work. the myurl.com/special URL got a 404 Not Found page.
itwasthewind
I realized I hadn't set the htaccess exactly like you mentioned, and I tried that, but now any url that doesn't have index.php in it (like myurl.com/special) just shows the default home controller. Any idea why that might be?
itwasthewind
with the htaccess it is set to do both myurl.com/index.php/special and myurl.com/special to the same page just making to urls cleaner. but I just realized what was going on i am so sorry i didn't read that right just remove the route you added and it will work perfectly i am sorry i thought you wanted to have myurl.com/special go to the default page the htaccess would have done it all along i am sorry i should have read it closer leave the htaccess alone and just remove the route you added. let me know if that worked.
BrandonS
i just double checked and that should work for you but make sure if you use the base_url() CI item that you have correctly set your base_url config item with the trailing / and that index.php is in the config under $config['index_page'] = "index.php" if you don't have them set and try to use any of the CI functions that build url for you those urls will not work.
BrandonS
Unfortunately, it's still not working. It redirects all requests to the home controller instead of going to the correct controller.
itwasthewind
and you removed the route in the routes.php file under config and left the htaccess there? is the "special" page it's own controller and it has a view of it's own not the same one as the default one? this is weird. I'd say go over your config and routes again then check your controllers and make sure there's not any mistakes it shouldn't do that.
BrandonS