Controller does not open in CodeIgniter - .htaccess

I have three controllers in CodeIgniter application - i, login and test.
All controllers perfectly work with /APP/index.php/ path - I can open /APP/index.php/i, /APP/index.php/login and /APP/index.php/test.
Controllers i and login work without index.php as well - /APP/i and /APP/login (I have a modified .htaccess file).
Controller test does NOT work without index.php - I get 404 error when I open /APP/test.
i is the default controller, but login is not default and it works (I thought it could be the problem).
When I added the 4th controller it also didn't work without index.php.
It gets even more weird - sometimes I can open /APP/test/some_function (and sometimes I cannot) and after this I can even open /APP/test. But then I cannot open it again this way. index.php way works all the time. Test controller has a really simple code...
So my question is what can possibly go wrong here and how can I make my controllers accessible without index.php?
Thank you very much for your help.
Below is my .htaccess file, I think the problem can be there:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /APP/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
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>

I now understand where the problem was, thanks to #w0rldart suggestion.
Actually, when you access the controller without index.php file, the main application folder becomes case-sensitive (APP in my question).
As I have a much longer application folder name and it has both lower and upper case letters, I just had it typed with one letter in the wrong case. For the default controller it does not matter and other controllers were accessed via the links where the case was correct.
And as test was accessed directly, this wrong case was giving me 404 error.
Via index.php path the case of the main application folder doesn't matter.

Related

Getting Symfony2 to Work on GoDaddy without app.php

I got my Symfony site installed on my GoDaddy production sever. It works fine when I use the url www.amcolan.info/Symphony/web/app.php/index. I want it to set it up to work as www.amcolan.info/index.
I altered the domain to point to the folder amcolan/Symfony/web via a GoDaddy control panel. It used to point to /amcolan which contains the working version of my site. After I got that set up, I installed the following .htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Explicitly disable rewriting for front controllers
RewriteRule ^/app_dev.php - [L]
RewriteRule ^/app.php - [L]
# Fix the bundles folder
RewriteRule ^bundles/(.*)$ bundles/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
# Change below before deploying to production
RewriteRule ^(.*)$ app.php [QSA,L]
#RewriteRule ^(.*)$ app_dev.php [QSA,L]
</IfModule>
I got the code from another related question on this site and modified to my purpose. The original was intended to live in the Symfony directory. I then cleared the production cache and attempted to access a page. It resulted in an Internal server error. Are there any errors in my code? You'd think this would be well documented by Sensio as I imagine 99% of all implementations would have to do this. Now I'm just getting bitter.
As suggested, I tried the following code that came with Symfony:
<IfModule mod_rewrite.c>
RewriteEngine On
<IfModule mod_vhost_alias.c>
RewriteBase /
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
I also tried:
<IfModule mod_rewrite.c>
RewriteEngine On
#<IfModule mod_vhost_alias.c>
#RewriteBase /
#</IfModule>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
I got the same error as before with both. To make sure I was getting to the directory, I added a index.php file to it and I was able to access it.
Update 2
After rooting around some more about goDaddy specific issues, I found a post on a goDaddy forum saying the RewriteEngine is already on. I deleted that code without affect. I then decided to try this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ http://www.amcolan.info/app.php/index [QSA,L]
That got it to work although I suspect not correctly. I had first tried it without the index after app.php/. That got me into symfony, but generated a get error. It appeared to be because of a null argument so I added the index. So, regardless of what someone might enter after www.amcolan.info, it's going to go to the index. Once you are on a Symfony page, you can navigate to other pages in the site. So now I have three new questions:
Any idea to where the web server is pointing when it reads the .htaccess file? You would think in the /web directory as that's where the file lives. Since just specifying app.php instead of the full path didn't work, it must not be the case. Would it be the base of my site?
After the successful redirection of www.amcolan.info, the resulting displayed URL is www.amcolan.info/app.php/index. I'd like to not see the /app.php in the URL, but I suspect that's not possible in as much as it is the actual destination. Is that a correct assumption?
How do I get www.amcolan.info/somepage to go to www.amcolan.info/app.php/somepage rather than the hard coded index?
Update 3
I thought I was stuck, but then I tried:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php/index [QSA,L]
That got rid of having to use the full URL and now the destination pages no longer display the app.php. I have a new problem: just specifying www.amcolan.info causes the get error mentioned above. Specifying www.amcolan.info/somepage no longer takes me to the index, but to the specified page. Stay tuned, I may be able to solve my last problem with a little more digging.
Update 4
I fixed the last problem by adding the following to my routing file:
nopage:
pattern: /
defaults: { _controller: ZetchoAmColAnBundle:Default:index }
requirements:
_method: GET
I think I'm a happy camper now.
index action is not necessary. You can easily configure any action for root routing as well.
Here is my full .htaccess file on GoDaddy that finally works. It works only for prod config (app.php), app_dev.php is not working well, but that is OK for me since it is used for production environment.
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php/ [QSA,L]
</IfModule>

Codeigniter and .htaccess file to ignore index.php

So I'm having a weird problem with .htaccess file and codeigniter. I have a wamp server on my local machine and there it works as expected. I have folders like that wamp/www/my_app and in my_app folder i have .htaccess file like that:
ErrorDocument 404 /index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /my_app/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
And in mine config.php gile i have:
$config['base_url']='';
$config['index_page']='';
$config['uri_protocol']='AUTO';
So i tried uploading that project to debian server on link like apps.myserver.com/my_app. I put my app to folder var/www/my_app. But the trick is that it doesn't work.
I have tried changing uri_protocol to all 5 options but no change. I also tried putting .htaccess file to var/www instead of var/www/my_app but no change either.
I keep getting that stupid 404 Not Found error when i try to access controlers without index.php prefix.
Any ideas?
EDIT: I checked phpinfo on server and found that mod_rewrite is in loaded modules.
The mod_rewrite module can have a couple different extensions. Your Debian server might use mod_rewrite.so instead of mod_rewrite.c.
That said, if you know mod_rewrite is enabled, and your application isn't being widely distributed and needs to check for it, you can safely eliminate the if checks.
You mentioned commenting out everything except lines 3 and 7 -- you commented out the RewriteBase definition, which broke stuff.
Try something simple, and work from there:
RewriteEngine On
RewriteBase /my_app/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Also, one variation you can do if an .htaccess is being weird, is to change everything after index.php to a query string. Note the ? here:
RewriteRule ^(.*)$ index.php?/$1 [L]
You may or may not to change the uri_protocol config setting in CodeIgniter. My guess is you specifically won't, but it's info for others who are having .htaccess issues.

Why am I getting a 404 error with this routing?

In my web root (var/www) I have the following in my .htaccess file:
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|vges|images|robots\.txt)
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /test/index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I then have a subdirectory (/var/www/test) with a codeigniter project. It also has a .htaccess file with the following:
RewriteEngine On
RewriteCond $1 !^(index\.php|quiz|vges|buddy|css|fonts|img|images|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
When I visit localhost I can view the test directory and when I go to the test directory I'm displayed with the default controller view. But when I visit localhost/test/controller I get a 404 not found error:
The requested URL /index.php/events was not found on this server.
Edit: I found the solution by changing the last line /var/www/vges.htaccess to:
RewriteRule ^(.*)$ test/index.php/$1 [L]
Although I suspect if I was to upload this test project to a server, where it is not a subdirectory then I may get an error. What's the best solution to this? What should
I change in my .htaccess file so
I can have subdirectories on my local machine
When I upload them as individual projects (such as the test project) to a server I don't have to modify the .htaccess file.
Thanks.
First, .htaccess files apply to the directory they're in, and every child directory. Your CodeIgniter-specific rules should be in the var/www/test/ CI directory -- usually wherever CI's index.php file is.
Second, your root .htaccess is kind of weird. RewriteCond conditions accumulate until there a RewriteRule rule fires, then they are reset. Your two %{REQUEST_URI} conditions conflict with each other, since the URI can't start with both. I'm not really sure what this .htaccess rule is doing, but if your URLs starting at the root have no bearing on your CI application, I don't think it's necessary to have it there in the first place. I can't say for sure without knowing your directory structure and how you want your website to function.
kjetilh is right - unless your environments share exactly the same settings, it's unlikely an .htaccess will be universally functional. Your best bet is to start them as simple as possible, and modify as necessary. A good starting .htaccess for CI 2.1.x is something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
You can add your assets directories or any paths that you don't want redirected in the third RewriteCond (remember to properly escape regular expression characters such as periods). A RewriteBase rule definitely comes in handy if CI is in a subdirectory. You also don't need the system and application folder references with 2.1.x, since those folders have their own .htaccess files blocking access to them.

Routing To A Folder In Cakephp's App/Webroot

I'm aware that there are a few questions on this subject already. I've trawled through them and can't make any of the proposed solutions work for me... maybe someone can help my specific problem?
I have a folder, parallax, in my app/webroot, containing index.html and associated files. This can be accessed just fine at /parallax/. However, if I try to access it at /parallax I get redirected to /app/webroot/parallax/, and ideally I'd like to suppress this behaviour.
Refactoring the whole thing as a CakePHP controller action isn't ideally something I want to get into (though if it might be simpler than I expect, let me know). The routes file doesn't as far as I know allow routing to a file in app/webroot.
The other port of call would seem to be the .htaccess file. I tried adding a rewrite rule:
RewritRule ^parallax$ parallax/index.html
or variations thereof, but the best I can get here is a page with broken images (whatever I'm ending up with, it doesn't seem to be able to find the images in the parallax/images subfolder any more).
Can anyone clear up my confusion and help me find the best route to /parallax giving the same result as /parallax/ does?
Please try the below .htaccess code in your root directory not in app directory.
<IfModule mod_rewrite.c>
RewriteEngine on
Rewriterule ^parallax/.*$ - [PT]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
This should help you. What ever request start with parallax will be skipped by the .htaccess rule. And rest of the things will work as it is.
.htaccess root thanks to Anh Pham for this link it works perfectly http://www.balistupa.com/blog/2010/08/how-to-redirect-appwebrootblog-into-blog-wordpress-cakephp/
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} !^/foldername.*
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^$ app/webroot/ [L]
RewriteCond %{REQUEST_URI} !^/foldername.*
RewriteCond %{REQUEST_URI} !-d
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
I tried to reproduce the situation but its working corretly for me. I'm using cakePhp 1.3 and both urls send me to index.html:
http://localhost:5013/parallax/
http://localhost:5013/parallax
My guess is that there's something weird on your .htaccess (the one located at /app/webroot/). Mine is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Basically, it tells the server that if the requested url is not a file (!-f) nor a directory (!-d), it will redirect the whole url as a parameter to /webroot/index.php , so cakephp will parse the url and call the controllers and all that..
Hope this helps
You should be able to put the parallax folder with a static index.html inside the app/webroot folder and have it be accessible by default at the /parallax/ url.
If you use relative paths you can even have you img/css for that page local to the folder and skip the usual cakephp paths entirely. No htaccess needed.

How do I do this with apache rewrites? (alter url if it doesn't contain a 1 of 2 strings)

I've installed a .htaccess file containing rewrite rules (as suggested by CodeIgniter) seen below.
How can I add to it so that any requests for urls that don't begin with members/ or admin/ get public/ put at the beginning?
i.e. if someone requests contact/, it should actually go for public/contact and so on unless they request members/something or admin/something.
.htaccess at present:
RewriteBase /
# Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^_system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# prevent user access to the application folder
RewriteCond %{REQUEST_URI} ^myapp.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# This snippet re-routes everything through index.php, unless
# it's being sent to resources, or searching for robots.text
# Add any OR's in here if you need other directly accessable Files/folders
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteRule ^(.*)$ index.php/$1
.htaccess - edited - this is how it looks now:
RewriteCond %{REQUEST_URI} ^_system.*
RewriteRule ^(.*)$ /index.php?/$1
RewriteCond %{REQUEST_URI} ^myapp.*
RewriteRule ^(.*)$ /index.php?/$1
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteRule ^(.*)$ index.php/$1
RewriteCond %{REQUEST_URI} !^/(members/|admin/|public/|index\.php) [NC]
RewriteRule ^(.*)$ /public/$1 [L]
It's returning a 404 for all requests at the moment. I'm sure I've got something wrong but don't know what :)
File structure is:
site_root/.htaccess
site_root/index.php
site_root/myapp/
You can prepend the regex with ! to mean "must not match". So you can achieve what you want like this:
RewriteCond %{REQUEST_URI} !^(public|members|admin)/ [NC]
RewriteRule ^(.*)$ /public/$1 [L]
Note: I'm adding a new answer because it is mostly unrelated to my old answer and is based on edits to your post. Leaving my old answer up for reference.
There are a number of problems here, but there is one big issue which needs resolving before it even makes sense to address the small problems. It appears that you are using a framework that works by routing everything (with a few exceptions) through index.php (which surely has its own internal router to the application controllers in myapp/). But you also say you want to route everything (with exceptions) through public/.
These are mutually exclusive possibilities -- you can't route all traffic through two places simultaneously. So I can go into fixing the above rules and show the code, but only after I know what the rules are supposed to be doing. Given what you said you want in your intro text, combined with what is already in your .htaccess, it's impossible to figure out what the combined rules are even supposed to do. I can edit this post with code details only after you answer some questions:
What framework are you using?
What is public/ supposed to be? Is it a directory (if so, what should it hold)? Or is something that is supposed to be routed to and handled by the application?
Do your current admin and members sections work by going to admin/ and members/ subdirectories? Or are these routed to and handled by the application?
Do you have any other important details about how the app is structured?

Resources