Kohana 3 deployment controller problems - model-view-controller

I have recently migrated my app from a windows localhost up to a linux host.
My problems is that I can't access a controller, I always keep getting the message: File not found.(From the web server)
And this is odd because the same code base is working on my localhost.
bootstrap.php:
Route::set('default', '(<controller>(/<action>(/<id>)(/<param1>)(/<param2>)))')
->defaults(array(
'controller' => 'user',
'action' => 'index',
'param1' =>'',
'param2' =>'',
));
.htaccess:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
The odd thing that happens here is that if I go to :
www.myapp.com/ then the default controller executes and it finds the user/index function.
But if I manually write it as www.myapp.com/user/index I keep getting the File not found message.
I have tried renaming the controller files with lowercase so I guess thats not the problem, besides if that would be the problem, the default route wouldn`t find it in the first place, right?
If for example I write in the default route:
Route::set('default', '(<controller>(/<action>(/<id>)(/<param1>)(/<param2>)))')
->defaults(array(
'controller' => 'user',
'action' => 'login',
'param1' =>'',
'param2' =>'',
));
And I accesss www.myapp.com , it would load the user function. I can't figure out what is wrong here, why when I write myapp.com/user/login it says file not found, but if I put it as the default route it opens it.

did you set the base_url in bootstrap.php? if no, you should set the base_url your_project_name/
if yes, you should set the RewriteBase /your_project_name/ also in your .htaccess

I found the solution in another thread related to this. You need to add this chunk of code into .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Hope it helps the others !

Related

Why do I get a 404 to laravel 5.7 public subfolders?

I have a Laravel 5.7 project and I can't seem to understand why I get a 404 error to the subfolders in the public directory.
Directory structure
public
icons
css
file.css
Example of routes that work just fine:
localhost/images/image.png
localhost/css/main.css
However when i try to request a file deeper that the first subdirectory, i get a 404. For exaple:
localhost/pugins/common/common.min.js
file located in public/pugins/common/common.min.js
localhost/icons/weather-icons/css/weather-icons.min.css
file located in public/icons/weather-icons/css/weather-icons.min.css
Anything after localhost/FOLDER/ gets a 404 eror, even if it exists.
Any ides why?
This is the default .htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I have noticed just now that the first level of the subdirectories of the public folder get a 403 (Forbidden) error while try to list that directory.
Example:
localhost/js => 403, Forbidden
localhost/js/dashboard/intro-inde.js => 404, Not found
localhost/js/script.js => 200, Working fine
Each file and directory exists in the examples above, and there is no problem with the permissions.
My guess is that it has something to do with the .htaccess, but i have no idea how that works >.>
I remembered i had no problems in previous projects using laravel 5.1 ~ 5.5. So I have copied the .htaccess file from those projects to this one. Now everything works fine, but for one folder and its subfolders : public/icons; I solved this problem by creating a new folder and moving everything from "icons" to "fonts-icons"
Behold the power of the new .htaccess. Maybe it will help someone.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
This one solved the problem...without the new for new routes. :D I still have no idea how it works and why, though.
Just for curiosity, maybe someone will enlighten me d=(^_^)=b
You should first define that route in the routes/web.php file.
Error 404 is when we do nnot find any pages. Which means we have to define route in our web.php file first so as to view desired content written in the blade.php file, This error may also appear if we have not saved the file in our views in .blade.php extension

mod_rewrite aliasing a subdirectory

I'm struggling with mod_rewrite as always. We have a number of client portals running through WordPress multisite, all accessed through a subdirectory: portal.
So for example: http://www.mydomain.com/portal/clientA/
I'd like to be able to get there just by typing http://www.mydomain.com/clientA/ and it would redirect me to http://www.mydomain.com/portal/clientA/
Here's what I have so far, and it's not producing any rewrite that I can tell:
RewriteCond %{REQUEST_URI} /portal/
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [S=1]
RewriteRule /clientA(/?) /portal/clientA/
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The second part I can't touch because WordPress needs it. My pattern is also trying to anticipate someone not putting in the trailing slash, hence the (/?)
EDIT: I should also note that I don't want to create a more general rule - I'm comfortable having to add a rewrite rule for each new client and increasing the S=x number each time.
EDIT (Aug 11), So after a little more puttering this is what my .htaccess is at:
RewriteEngine On
RewriteRule ^clientA(/?) /portal/clientA/ [R]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Needless to say it doesn't work. However, the first part works IF I delete the entire WordPress section. I need them BOTH to work simultaneously. WHAT is it about the WordPress piece that is causing the failure of the first section? I suppose it's the combination of RewriteBase and the very last rule which aliases anything else to /index.php, which frankly is a bit of a bummer. In fact I don't truly understand how that rule could even work in a multisite context, and yet it seems to.
FINAL SOLUTION
thanks to LazyOne for the correct answer! For others' reference, the final solution I used was:
RewriteEngine On
RewriteRule ^clientA(/.+)? /portal/clientA$1 [R,L]
RewriteRule ^clientB(/.+)? /portal/clientB$1 [R,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
As simple as this:
RewriteCond %{REQUEST_URI} !^/portal/
RewriteRule (.*) /portal/$1 [L]
It will rewrite (internal redirect) all requests into /portal/ folder (e.g. /clientA/something => /portal/clientA/something).
If you need to do it for some clients only (or, better say, only specific folders that are clients while still having some general/common folders as is), you can use this rule for each client:
RewriteRule ^clientA(.*) /portal/clientA$1 [L]
So that .htaccess will look like this:
RewriteRule ^clientA(.*) /portal/clientA$1 [L]
RewriteRule ^clientB(.*) /portal/clientB$1 [L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I posted part of this as a comment, but figured it might be more clear for people who land here to do this as an answer. To the OPs point, he found a way to do this using the [R] (redirect) line, but this eliminates the subdirectory URL structure you created that would be preferable in most URL Rewrites. So, the answer previously posted is right, I'm not contesting that, but depending on your implementation, you may still get WordPress 404 errors. Here is a solution to my situation, which I think might be more common.
In my case, I needed a URL structure like this:
http://mysite.com/p/profile_name/
Each user who comes to register can create his/her own profile on the fly, and aside from a few modifications to the content, for the most part all of the WordPress content at the root is what will be displayed. Essentially, I need this:
http://mysite.com/p/profile_name/(.*)
To be rewritten to this:
http://mysite.com/$1
This is the .htaccess code posted in the other answer that WILL handle that rule correctly:
RewriteRule ^p/([-a-zA-Z0-9_]+)(/.*) $2 [L]
The problem with this is that WordPress does not care about your rewrite in terms of understanding what $2 is because WordPress uses $_SERVER['REQUEST_URI'], which no matter what you rewrite to, it's always what's in the user's browser window. The OP found a way to get around this by using the [R] option, but that causes you to lose your URL:
RewriteRule ^p/([-a-zA-Z0-9_]+)(/.*) $2 [R,L]
Redirects:
http://mysite.com/p/profile/(.*)
To:
http://mysite.com/$1
But, the user loses his unique URL this way. At best you can add a query string to at least retain the data, but then you lose the point of the pretty URLs to begin with.
I did come up with a solution; however, it involves hacking the WordPress include files :( If someone has a better way, please update. Here we go:
SOLUTION
I set my .htaccess file equal to this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
#My addition:
RewriteRule ^p/([-a-zA-Z0-9_]+)(/.*) $2 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Then, I knew WordPress used the REQUEST_URI variable, so I did a recursive search, and located this line of code in /wp-includes/class-wp.php (line 147 v3.4.2):
$req_uri = $_SERVER['REQUEST_URI'];
I changed it to this:
$req_uri = preg_replace(#'/^\/?p\/([-a-zA-Z0-9_]+)\//', '', $_SERVER['REQUEST_URI']);
Which basically just tricks WordPress by filtering out the profile stuff at the beginning of the URI.
Lastly, I also needed a solution for the links within the site. For that, I added this filter:
NOTE: Some of these regexes might be a little bonkers; I was filtering out the site specific stuff, so please use this as a concept and not a copy/paste.
function mysite_wp_make_link_relative( $link ) {
$sBaseUrl = (preg_match('/^\/(p\/[-a-zA-Z0-9_]+\/)(.*)/', $_SERVER['REQUEST_URI'], $matches)) ? $matches[1] : '';
return preg_replace( '|https?://[^/]+/(.*)|i', '/' . $sBaseUrl . '$1', $link );
}
function rw_relative_urls() {
$filters = array(
'page_link', // Page link
'home_url',
'site_url',
'get_site_url',
'home_link',
);
foreach ( $filters as $filter ) {
add_filter( $filter, 'mysite_wp_make_link_relative' );
}
}
Some of those filters might not be relevant; I'm pretty sure page_link and home_url are the only important ones. Anyway, you need that code for your internal linking to work.
I hope that helps and if anyone has any comments suggestions for improving this, I would greatly appreciate it.

Codeigniter - no input file specified

I am a beginner in Codeigniter and I saw a CI tutorial and was just trying to do a simple thing. I downloaded the CI and added this file to controller directory, but it won't work.
<?php
class site extends CI_Controller
{
public function index()
{
echo "Hello World";
}
function dosomething()
{
echo "Do Something";
}
}
?>
When I try to access it using http://..../index.php/site I get the output ... "no input file specified" .... by the way, I named the file site.php
Just add the ? sign after index.php in the .htaccess file :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
and it would work !
Godaddy hosting it seems fixed on .htaccess, myself it is working
RewriteRule ^(.*)$ index.php/$1 [L]
to
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
I found the answer to this question here..... The problem was hosting server... I thank all who tried .... Hope this will help others
Godaddy Installation Tips
RewriteEngine, DirectoryIndex in .htaccess file of CodeIgniter apps
I just changed the .htaccess file contents and as shown in the following links answer. And tried refreshing the page (which didn't work, and couldn't find the request to my controller) it worked.
Then just because of my doubt I undone the changes I did to my .htaccess inside my public_html folder back to original .htaccess content. So it's now as follows (which is originally it was):
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
And now also it works.
Hint: Seems like before the Rewrite Rules haven't been clearly setup within the Server context.
My file structure is as follows:
/
|- gheapp
| |- application
| L- system
|
|- public_html
| |- .htaccess
| L- index.php
And in the index.php I have set up the following paths to the system and the application:
$system_path = '../gheapp/system';
$application_folder = '../gheapp/application';
Note: by doing so, our application source code becomes hidden to the public at first.
Please, if you guys find anything wrong with my answer, comment and re-correct me!
Hope beginners would find this answer helpful.
Thanks!
My site is hosted on MochaHost, i had a tough time to setup the .htaccess file so that i can remove the index.php from my urls. However, after some googling, i combined the answer on this thread and other answers. My final working .htaccess file has the following contents:
<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On
# If your website begins from a folder e.g localhost/my_project then
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
RewriteBase /
# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(application|system|private|logs)
# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|app_upload|assets|css|js|images)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php?/$1 [PT,L]
</IfModule>
One of my Codeigniter apps started returning this error after i restarted my server.
When I checked the Codeigniter error log it says something like:
"...[error] 879#0: *273 FastCGI sent in stderr: "PHP message: PHP Warning: Unknown: open_basedir restriction in effect. File(/pathToWebsiteRootFolder/index.php) is not within the allowed path(s): ".
So I added this:
open_basedir= /pathToWebsiteRootFolder/index.php:
To a user.ini file I created in my website root folder.
And this Solved it.
FYI: Im using an NGINX web server.
However, Its strange because I didn't have to do this for the other Apps on the same server.

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]

codeigniter 404 on existing controller

I am facing a different problem in codeigniter.
When I try to access the videos page in my site, it will redirect to 404.shtml page. But cvideos.php file exists in my controllers folder.
If I access like this http://domain-name/videos , then it will be redirected like this
//domain-name/500.shtml
And If I access the same page like this //domain-name/videos/myvideos, then then it will be redirected like this
//domain-name/404.shtml
Also, If I change the controller name from videos to some other name like videoss it works fine. Can anyone tell wats the issue.
I used this line in my .htaccess also just for testing. But no use.
RewriteRule ^videos/$ index.php/videoss/ [L]
Your controller needs to be the same name as the class it contains.
hence -
<?php
controller Videos extends Controller {
/* bla */
}
?>
should be saved as:
videos.php in the "controllers" directory.
Nothing else will work.
also your rewrite rule has two "s"'s, but that might be intentional.
and it looks like what you are trying to do with .htaccess can be achieved with CI's routing
Edit: .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
please note I did not create this - it was found by scouring the CI forums. It's a rather thorough htaccess however.
Don't forget to set the value of "index.php" to "" in your config.
base_url is case sensitive
localhost/site/controller = c:...\site
localhost/SITE/controller = c:...\SITE
make sure you are not missing .htaccess file in your root directory. Or check for its accuracy. Make sure uri_protocol and base_url are set correctly in config.php.
for sample, i am copying my .htaccess file here
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /your_dir
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>
I used this to get rid of index.php in url.
I had a similar problem when transferring a CI site from WAMPP to a LAMPP that someone else had made. It would seem that WAMPP was configured to be case-insensitive, as when I converted all my controller/model/view file names and all php strings containing controller/model/view names to lowercase it worked perfectly.

Resources