Mod Rewrite Newbie in CodeIgniter - mod-rewrite

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.

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

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>

Related

CodeIgniter - calling main controller resets url to server root

I have a little problem with codeigniter and the default controller behavior.
My CI resides in a subdir on the development environment at localhost/web/new_info and the same path for production like 192.168.30.108/web/new_info.
Always when calling the url like
localhost/web/new_info/welcome
the page takes me to the server root, e.g. localhost/.
But I can call the "welcome" controller via:
localhost/web/new_info/
localhost/web/new_info/index.php/welcome
and I can call welcome's methods as well like
localhost/web/new_info/welcome/logout
There is another subdir in my controllers called settings and contains another welcome.php intself. I can access this controller directly without problems and the page stays there
localhost/web/new_info/settings/welcome
There is .htaccess and mod_rewrite set up according to
http://www.farinspace.com/codeigniter-htaccess-file/.
.htaccess
SetEnvIf Host 192.168.30.108$ CI_ENV=production
SetEnvIf Host localhost$ CI_ENV=development
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /web/new_info/
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
in config/development/config.php I have
...
$config['base_url'] = 'http://localhost/web/new_info/';
$config['index_page'] = '';
...
and generic routes
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Xampp and the real server running apache2 act the same way. Even after completely disabling mod_rewrite and calling the welcome controller redirects to server root.
Currently I am using CI 3.0.3 and tested with the latest 3.0.6 as well with the same behavior.
What I want is to call localhost/web/new_info/welcome and stay there..
Thank you!
Ok, found out by myself.. dumb copy paste of .htaccess.
Namely the line
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
has to be
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ /web/new_info/ [L,R=301]
in my case. The author of the htaccess file didn't mention it on his page.
But changing the htaccess file only did not help. I had to completely restart my session, otherwise changes were not applied. It's a bit unusual for me, I thought changes in this file were instant.
Nevertheless, it's fine now.

CodeIgniter and specific rewrite rule

On my CodeIgniter site, I would like to add a specific rewrite rule, so that this url
http://www.exemple.com/cache.manifest
would rewrite to
http://www.exemple.com/controller/manifest
(because Safari 7 seems to only accept .manifest files for ApplicationCache)
So I try to add this line to my htaccess
RewriteRule ^cache.manifest$ controller/manifest
I added it before the other rewrite rules :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^cache.manifest$ controller/manifest
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
But it returns a 404. If I change the line to
RewriteRule ^cache.manifest$ test.html
it works. So the first part on my rule is correct.
If I try to access directly to www.example.com/controller/manifest, it works to, so my url is correct.
I tried also
RewriteRule ^cache.manifest$ index.php/controller/manifest [L]
But it doesn't work either…
Any clue ?
Thanks a lot
I tried some tests on my local server and I think the following might work:
RewriteRule ^cache.manifest$ /index.php/controller/manifest [R,L]
I am not entirely sure if you need the leading "/" or "/index.php", so you
may need to experiment.
You need the [R] flag to force a redirect. In this situation, you want Apache
to look for the string cache.manifest in the URL, and then go to the CI page
controller/manifest.
It appears that you need to explicitly set the redirect.
Please let me know if this works. Good luck!

Codeigniter redirect solution

Trying to get
www.example.com/home
to go directly to
www.example.com/
What I've tried:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\www.example\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]
What am I doing wrong?
but its website rotating always .
Redirect 301 /home http://www.example.com/
If what you are saying is that you want the index function in your "home" controller to be the default homepage, you can set that in routes.php under the route['default_controller'] parameter. That way you don't need to worry about the redirect at all. If you want to remove /home anytime it's typed in, you can use a mod-rewrite like you do to remove index.php from the uri. Check out the docs for more info http://ellislab.com/codeigniter/user-guide/general/urls.html

404 is given when trying to access existing controller in CodeIgniter

I am working on a fairly new install of CodeIgniter 2.0.
I have a htaccess-file containg this code
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Going to the URL http://www.estiem.no/ESTIEM/CI/ works,
but http://www.estiem.no/ESTIEM/CI/site/index does not. It gives a 404. The controller Site exists, and contains the method 'index'
Any ideas what might be wrong?
You need to remove / from index.php
RewriteRule ^(.*)$ index.php/$1 [L]
It is a htaccess problem, back when everyone passed that code around we found that a lot of hosts didn't like it and you could hack it with a ? or this or that until it ended up looking like a jigsaw puzzle (the condition that is) but using a brilliant htaccess (not sure who the first was but like 500 have taken credit) you will never have to change it again (well not never but it will work 99% of the time un edited):
# 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>
Trust me I have had the same issues and spend hours on google when i should have come here or even the CI wiki (since that is the first place I seen it), you would not believe how many issues are solved in CI with just that bit of htaccess. they really should add it to the repo.
I just noticed that since you have your CI install in a subfolder, you need to account for that in the .htaccess rewrite:
/ESTIEM/CI/
So your .htaccess will look like:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ESTIEM/CI/index.php/$1 [L]
That should fix it (sorry late edit, due to #BrianOrtiz 's comment)
Try to access the controller with: localhost/site/index.php/controller
If that does not work maybe because the CI is case sensitive
localhost/site/controller == c:...\site\controller.php
localhost/SITE/controller == c:...\SITE\controller.php
else
<?php
controller Mycontroller extends Controller {
}
?>
will be named mycontroller.php in the controllers folder
If all else fails replace your htaccess by this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

Changing uri_protocol to PATH_INFO breaks my CodeIgniter app?

I'm setting up Haughin's Twitter OAuth Library for CodeIgniter, and one requirement is to change the uri_protocol in config.php to PATH_INFO, which seems to be breaking my app in the way that all requests load the home controller (ex. Navigating to http://dev.myapp.com/login would normally take my to the Login controller, but it's just showing the default controller.)
Any ideas what would be causing this, or maybe an Apache2 configuration that's messed up?
Its to do with URL rewrites. Check your .htaccess files to see if you have the correct rewrite rules. Here is something that works for me when using PATH_INFO.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
The above should work for you without any modification. If you're hosting in a folder lets say like http://myserver.com/my_app/ then change /index.php to /my_app/index.php in both places.

Resources