UPDATE
If I browse url like
localhost/myapp/index.php/about_us/index
it works. What is this index.php in url? Do I need to mention it in some config file so that it gets appended and forms correct url in menu/links on site?
Original Question
I have no knowledge of PHP but I got a project which in php (codeigniter) to convert in Ruby on Rails.
I could set up db and application well, when I browse application with base url(without mentioning controller & action) it loads page properly. But as soon as I mention url like
localhost/myapp/home/index
it shows message
The requested URL was not found on this server.
If I change default controller to anything in routes.php that page with index method works fine but not with explicit mentioning controller and action.
I am not sure what is the issue, I don't know how routing works in php :(
Any help will be appreciated.
In CodeIgniter, all requests are directed through the index.php file, which acts like a front controller.
index.php must be present in the URL unless you have created a solution to eliminate it. When using Apache, that is typically done with an .htaccess file. There are hundreds of articles and questions regarding this technique -- certainly you can find something to help you.
In regards to URLs and a config option for defining index.php, CodeIgniter URL helper functions such as site_url() utilize the config setting $config['index_page'] found in application/config/config.php. If you remove index.php from your URLs using an .htaccess solution, you can make this setting blank:
$config['index_page'] = '';
This setting is useful for when you want to rename index.php to something else, as well (not very common).
It seems that you had not configured your web server properly. See this question for details for Apache: routing problem in codeigniter
And here are rules for nginx: http://wiki.nginx.org/Codeigniter
Related
I have used Slim Framework several times without any problem but now I'm trying to set a route to match URL that ends with .html and render a certain view, but the framework return a 404 Not Found error, the route is ignored and it seems to me that just checks if there is a file named like that, and since there isn't, simply throw the 404.
The route I'm trying to set is this:
$app->get('/{slug}.html', BusinessController::class . ':business');
This is possible in Laravel and that's the reason I'm a bit confused. Maybe someone stumbled upon this issue and know a solution.
Firstly to achieve this you will need to disable your web server (apache, nginx or other) from serving static html pages for your project. Typically a url that ends with .html will cause web servers to check for the corresponding static html file.
Then you need to add rewrite logic in .htaccess file to make sure all *.html routes are passed to your frameworks route handler like index.php or server.php (I am not familiar with Slim). Then its a matter of how you have configured your framework to handle there routes.
I'm starting to implement a php application on remote server using codeigniter and netbeans. I don't understand why I still have (404 not found) even if I did all the configuration. The connection to the server is OK, I can upload the files. In $config['base_url'], I tried the adress of server, the path to index.html, nothing... with no results.
I actually don't know where is the problem and which file I must change, If someone can help me please to find a solution.
Thank's a lot for yous answers!
It is kinda hard to troubleshoot your question without seeing your configuration. I would say to first double-check that your $config['base_url'] like you mentioned, but set it to whatever the site name/address is. For example, if I am just running something locally on port 8888, it would look like this
$config['base_url'] = 'http://localhost:8888/';
So if your site was something like wwww.example.com it would be
$config['base_url'] = 'http://www.example.com/';
Also double-check your .htaccess file to re-write access if you want to remove 'index.php' from the URL. What web server are you using for your site?
You say "the path to index.html".
This is not how CI works. You shouldn't access views directly. Try accessing them using your Controllers. For example you can have a controller 'Welcome' (which is the standard controller when you download the framework). It can look something like this:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
function index()
{
$this->load->view('welcome_message');
}
}
This controller should be located in
application/controllers and it will load a view in application/views. In this case this will load 'application/views/welcome_message.php'. You can define the url for the controller in application/config/routes.php. By default the controller 'Welcome' is called when you just download the framework. Also you talk about the .htaccess file. You can use an htaccess file if you want to remove index.php? from the url. See an answer to this question here: CodeIgniter removing index.php from urlI really recommend to watch some CI basics tutorials before kicking off with the framework. You can get a grasp of how the framework and its folder structure works within a few hours.
**Edit **Perhaps the problem isn't with codeigniter but with your webserver, you can remove your codeingiter installation and add a phpinfo file. This way you can see which php settings your server has (if php is enabled at all). Also note that nginx doesn't work with .htaccess, do if you're using an nginx server you should add a rewrite rule in your config file.
Some hosting providers have Mod_Rewrite disabled by default. use phpinfo() to check if it's enabled.
I have an issue making my cakephp project work on my shared hosting server.
I followed all the needed steps so it can work on local, and it's working. Once I upload on the server, in a specific domain name, only the homepage works, once I want to call another controller, i get the "404 - File or directory not found." error.
the project is hosted at : fme.tahrijouti.com. the domain redirects to the webroot folder in the cakephp structure.
My controller is : posts. I can make it work if I change the URL by specifying the controller & action, you can see it here.
I have checked and the rewrite module is on.
Can you please help me with that ?
Please have a look on CakePHP default routing. And don't forget to fix all your links.
Your links should have proper addresses. Just look on your Posts's link. These are like this http://fme.tahrijouti.com/index.php/posts/posts/view/2 but it should be http://fme.tahrijouti.com/index.php/posts/view/2. Remember after your host name there exist controller then a function of that controller then argument of that function like this: [host]/[controller]/[function]/[argument1]/[argument2]. Hope it helps. I think you can read cookbook another bit as most things are clear there from my experience.
As I don't have your codes of controller I cannot say more.
I am using hmvc for creating a register page :-
In the url now I am typing :- http://localhost/CI/index.php/user/registration
user is the module name
controller :- registration.php
<?php
class Registration extends MX_Controller{
function index() {
$this->load->view('homepage');
}
function register(){
$this->load->view('registrationPage');
}
}
?>
view :- homepage.php
<html>
<body>
register
</body>
</html>
the problem is that in my url i have to type http://localhost/CI/index.php/user/registration/register for coming to registration page.
I want http://localhost/CI/index.php/user/register how can we do this.?
setting base_url is not working and setting routes ia also not working.
Is this possible to go to any page without giving module name in the url..?
Edit you routes.php file under config folder
$route['user/register'] = 'user/registration/register';
You could use url rewrite in .htaccess file
See url rewriting without htaccess
and how to do url-rewriting in PHP
In CodeIgniter, .htaccess is present in
system/application/config/config.php
Get more info here as I am not aware of CodeIgniter
You should create a proper URL for your site by adding an entry in your host file. For example:
127.0.0.1 http://mysitename
or
127.0.0.1 http://localhost.mysitename
Google up how to set a virtual host in apache. You should be able to find plenty of articles about that. Now edit your apache config to enable virtual hosts. With this feature enabled you will create a virtual host entry for your site. The virtual host tells apache to map the URL you've chosen to the specific directory path of your project files. (This way the actual directory path is not part of your URL.)
Next you should edit your .htaccess so that requests are always routed through the index.php front controller. Once this is done you will not have to use '/index.php/' as part of each URL. Read the section in the Code Igniter user guide called Code Igniter URLs. It has sample .htaccess configurations. Moreover, it explains how Code Igniter URLs work.
In order for CI to work with the virtual host, you must set the base URL in your CI config file to match the v-host URL.
Once you've completed all of this, you will need to restart apache.
Now you can just name your controllers and methods in accordance with the URL structure you want. In this case, a controller called user.php with a method called register.
Is it a must to use HMVC in your project? Otherwise go for simple Codeigniter way because that is what you expect in this situation.
I have installed zend framework on my local machine. I have configured a vhost in httpd.conf and have added a line in my hosts file (127.0.0.1 mysite). I am running windows 7. Everything works perfect. The problem is when i upload on a hosting server the paths get mixed up.
I am uploading on a remote dir called zf-framework. To access the index page i need to type this url: http://mysite/zf-framework/public. It displays the index page but when i press any links on the page they get mixed up and end up being something like http://mysite/controller/action when in fact it should be http://mysite/zf-framework/public/controller/action. I have found a work-around for this situation...to use echo $this->baseUrl(link) for any links i have in the layout.phtml. The problem is more serious when it comes to submitting forms. I can't use baseUrl there....or i don't know how to use it. Is there a way to write some general config stuff so that this could be automatically resolved by the framework. Let's say to write something in index.php or bootstrap.php that will fix the paths automatically?
If you're using Zend_Application, then add the following to your configs/application.ini file.
resources.frontController.baseUrl = "/your-path-here"
If you're not using Zend_Application, then do this in your bootstrap, or index.php file.
$front = Zend_Controller_Front::getInstance();
$front->setBaseUrl('/your-path-here');
You won't have to use $this->baseUrl() when submitting a form to the same action and controller (just leave out the action attribute in the form tag), or when using the Redirector action helper. However, links in your view scripts will require you to $this->baseUrl('/url-without-base'), which doesn't seem too bad to me.
I am not 100% on this, but if you specify the route in your routes.ini as zf-framework/public/Controller/Action etc this should fix your issue.
I would see this as a bandaid, but I am not 100% sure on how to properly fix your issue other then you modifying the vhosts file on the remote server to set a document root to the public folder. If that is not an option, well the above should work, but know that all of your files are potentially accessible from everyone (at least your folder structure). I am not sure what harm this can do (if any) other then if your database schema is in the /data directory.
It is better to try and get the public set as the web root, if possible.