Subfolder routing not working - php

I am having trouble in routing my files. I need to access the new_account subfolder and the controllers inside it...
Here is the structure:
controllers
---new_account
---------step1.php
---------step2.php
---accounts.php
---pages.php
This is the content of my .htaccess file located outside application
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
And here is my routes.php
$route['default_controller'] = 'pages/show_page/home';
$route['pages/(:any)'] = 'pages/show_page/$1';
$route['accounts'] = 'accounts';
$route['new_account/(:any)'] = "new_account";
$route['(:any)'] = 'pages/show_page/$1';
$route['404_override'] = '';
I also use $config['uri_protocol'] = 'REQUEST_URI';
My pages controller is working perfectly, even as direct links e.g. <?php echo base_url(); ?>home redirects to ../pages/home.php.
The accounts.php controller calls new_account/user_home.php that contains guidelines and a link to the actual form. The link is <?php base_url();>new_account/step1 and it does not work which should have because of $route['new_account/(:any)'] = "new_account";.
Whenever I click the link to new_account/step1, I get redirected to the main home page.
I really want to access app_name/new_account/step1 and the others in the directory...Did I miss something in these files or an integral step?
NOTE:
I also tried this subfolder routing extension by inserting MY_Router.php in the application/libraries folder but no changes happened. I then moved it into application/core but all I get is a server error.

Comment this line in your routes.php:
$route['new_account/(:any)'] = "new_account";
Codeigniter default routes should work for http://YOUR_SITE/new_account/step1 cause you have a controller file called step1.php inside the folder new_account. Also check that the name of the class of your controller inside step1.php is Step1

Related

htaccess rule not working when adding slash

My main domain points to /html folder which has nothing in it but a .htaccess file.
I have a CodeIgniter website in /html/manage folder.
This is htaccess file content
RewriteEngine on
RewriteBase /
RewriteRule ^pg/(.*)$ /manage/product_groups [L,QSA]
This does not work, it shows 404 error page of codeigniter.
(If I visit website.com/manage/product_groups directly from browser, it works fine)
If I do RewriteRule ^pg/(.*)$ /manage [L,QSA] then it works fine, actually this page has login page of CI.
My main goal is to redirect every request redirect from website.com/pg/ to website.com/manage/product_groups
I didn't know if you had a special case or not.
Just referring to the routing doc examples.
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['pg'] = 'manage/product_groups';
or
$route['pg/(:any)'] = 'manage/product_groups';
Might have to experiment a bit.

How to load up pages in different directory without it affecting URL

My file structure usually goes something like this -
/images/
/css/
init.php
header.php
page1.php
page2.php
etc ...
After basically creating more and more pages, its becoming much more difficult to manage them because it's essentially turned the root directory into a massive list of pages.
How can I make it so the pages are placed in a folder along with its associated category like this -
/pages/category1/page1.php
/pages/category1/page2.php
/pages/category2/page3.php
without it affecting the URL so it still looks like http://domain.com/page1.php
Will editing the .htaccess be the most effective method of doing this? or is there another way?
I created a singular php file in the root directory that basically got a PHP page from a $_GET request and then checked to see if the PHP page exists. If it did then I used REQUIRE_ONCE to get the page.
//Get page from $_GET request
if (isset($_GET['page'])){
$page = $_GET['page'];
}
//Create path including the page
$url = ROOT_DIR . "/pages/".$page;
//If the page exists include it, if not then show a 404 page.
if(file_exists($url)){
REQUIRE_ONCE $url;
}else{
REQUIRE_ONCE "/pages/404.php";
}
This allows me to access login.php in the /pages/ directory using
http://domain.com/index.php?page=login.php
I then just rewrote the URL with .htaccess to make it more attractive.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1.php [QSA,L]
so that allows me to use
http://domain.com/login

.htaccess rewrite to access php include in subfolder

I have this URL that give me two main parameters, folder and page, and som other tahat can variable to be present or not:
http://www.domain.com/adm/master.php?f=folder&p=page&otherparam1=somthing&otherparm2=somthing&otherparm3=somthing[...]
in my master.php-file I include a php-file based on these parameters:
$folder = $_GET['f'];
$page = $_GET['p'];
if(empty($page)) {
$page = 'dashboard';
}
$path = $folder.'/'.$page;
include_once 'pages/'.$path.'.php?otherparm1=somthing&otherparm2=somthing&otherparm3=somthing[...]';
The otherparam is to be multiple parameters given i the URL.
I want to rewrtite the URL to show somthing like this:
www.domain.com/adm/folder/page/somthing/somthing/somthing[...]
I have searched the web, but not been able to find a solution.
If you want to do it with simple .htacces then you can use following .htacces code:
RewriteEngine on
RewriteCond $1 !^(index\.php|css|favicon.ico|fonts|images|js|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Basically what this code do is overwrite your request to your domain (ex: localhost/mysite), instead accessing folder or file it will access your index.php. Except for index.php, favoicon.ico, robots.txt files and css,fonts,image and js folder or what you define in second line on my code above.
Try this in your Root/.htaccess
RewriteEngine On
RewriteRule ^adm/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /adm/master.php?f=$1&p=$2&otherparam1=$3&otherparm2=$4&otherparm3=$5 [NC,L]
$1 ,$2,$3... contains whateve value was matched in regex capture groups "([^/]+)".
This will rewrite
example.com/adm/folder/page/foo/bar/foobar
to
example.com/adm/master.php?f=folder&p=page&otherparam1=foo&otherparm2=bar&otherparm3=foobar
without changing the url in browser address bar.

home.php not being accessed in codeigniter

I have a few sites set up on CI but latest one isn’t cooperating: browsing the domain doesn’t trigger home.php. I get “No input file specified”. I must be missing? (btw, when I put a test index.php file in root the echo code does render)
Here's what I've got set up -
//permissions:
//all subdirectories & file set to 755
//config.php:
$config[‘base_url’] = ‘http://example.com’;
$config[‘index_page’] = ‘home’;
//htaccess in root:
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]
//routes.php:
$route[‘default_controller’] = “home”;
$route[‘404_override’] = ‘’;
//home.php:
class Home extends CI_Controller {
public function index()
{
$this->load->library('session');
$this->load->view('main_view');
}
}
My settings seem to mirror those of my other the working CI sites but I’m definitely missing something. Would appreciate feedback on what else I should look for. Thanks
Since you're removing index.php from your URI, you don't need to specify an index_page in config.php. Setting $config['index_page'] = ''; should solve your problem. You should be fine leaving your $route['default_controller'] as it is, since CodeIgniter automatically defaults to the index method.
Your 'default_controller' config must be 'home/index'
Also be sure you placed the default config on the bottom of the file. In the CI routes files, always start from the most specific to the most general ...

remove controller name from url in codeigniter with routes

I know this has been asked a bunch of times, but I can't make it work at all. I'm trying to remove the index.php and the controller name (I only have one controller) from the URL.
So far, I was able to remove the index.php, but I still can't remove the controller name
Here is my htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|static|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Here is the top part of my config file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['base_url'] = '/main/';
$config['index_page'] = '';
And I put this as my last line in my routes file:
$routes['([a-z\_]+)$'] = "main/$1";
I'm using BlueHost, and the site is an addon domain to that account. Not sure if that makes any difference. But as of now, I was able to remove the index.php, but I want to remove "main" as well.
Any help would be great!
Thanks,
I'm not where I can try this to see if it works, but what if you leave the .htaccess as you have it and use this in your routes file?
$routes['(:any)'] = "main/$1";
One of the things I see here that you can change are:
$config['base_url'] = 'http://your-site.com/';
And in your routes.php
$route['default_controller'] = 'main';

Categories