How to setup routes for simple frontend routing - codeigniter

i'm a newbie in codeigniter.
I want to create a new page for the frontend - mynewpage.php.
The index.php in frontend will work fine.
So i do the following steps
In the Frontend.php Controller i add this
public function mynewpage()
{
$this->view = 'frontend/mynewpage';
$this->layout();
}
the mynewpage.php is still inside the "views/frontend/" folder
In the routes.php is this part for the frontend i think
$route['/'] = "frontend/";
$route['/(:any)'] = "frontend/$1";
This is inside my htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
AddDefaultCharset utf-8
So if i enter my webadress like
-> www.example.com the index.php will be display and the adressfield show www.example.com
-> if i try www.example.com/mynewpage.php i get Error 404 - Page not found
-> if i try www.example.com/mynewpage i get Error 404 - Page not found
-> if i try www.example.com/frontend/mynewpage.php i get Error 404 - Page not found
-> if i try www.example.com/frontend/mynewpage the page will be show
What i have to change or do that the mynewpage.php will be show on enter the adress without the "/fontend/" = www.exapmle.com/mynewpage and as mynewpage.php?
Thanks a lot

You need to write a route for your mynewpage, something like:
$route['mynewpage'] = "frontend/mynewpage";
right now you have a route for frontend, but frontend is your controller. No need to route that.
see more about CI URI Routing

Related

AJAX-Request: /wp-admin/admin-ajax.php 403

I created a wordpress plugin for calculating costs. The form have only one input-field and a submit button. On submitting the form I'd like to create a ajax request. So, I call the /wp-admin/admin-ajax.php- Url ,but I get the forbidden-Error-Code 403. I thought the entries in the htaccess are wrong, but I get the same error after changing the file and restarting the server.
Do you have any idea what's going on here?
# 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]
# admin-ajax.php & post.php skip/bypass rule
RewriteCond %{REQUEST_URI} (admin-ajax\.php|post\.php) [NC]
RewriteRule . - [S=2]
</IfModule>
# END WordPress
It gives 403 forbidden error if you didn't declare that ajax call is allowed.
//allow this particular AJAX function for logged in users
add_action('wp_ajax_myajax', 'myajax');
//allow this particular AJAX function for non-logged in users
add_action('wp_ajax_nopriv_myajax', 'myajax');
function myajax() {
//your code
}
what worked for me was to disable mod sec on the server!

codeigniter page not found on live server

codeigniter page not found showing but working fine in local server.
I had uploaded codeigniter file of one basic simple page on live server but there it is showing no page found and same file it is working on local server.
In that I had
mine.php ---- controller
index.php --- view
I loaded in routes default controller - mine in config base url- domain.com removed index.php also
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
can I know please why it is showing page not found, might be some routing issue but I had loaded default controller also. Please help out on this
First of all start error logging properly so you can see the actuall error.
Change your file name's first letter capital. This is mendetory as explained here ci3 manual - controllers. It says class name as well as file name must start with capital. ( If your Dev machine is windows then it will not show errors if you don't follow this rule. Because window's file system is case insensitive. But on your server it is linux.so it will show error)
If you still don't get it working then try access the url with index.php in it. Temporaryly disable .htaccess .If you get it working then may be you have problem in . htaccess. Try the following link to properly remove index.php from your url. Expression engine forum .This htaccess worked for me on all different server I have used.
add this in .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|images|asset|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

403 error on moving codeigniter to live server

I have a Codeigniter site that works fine on my local XAMPP server, but now I've moved it to shared hosting I get a 403 forbidden error when I click on any link. The homepage displays fine, but any page with a slug produces the 403 error. E.g.
http://xxxxxxxxxxx/~test/
works fine
http://xxxxxxxxxxx/~test/about-us
produces the 403 error.
I saw this question, but my application folder and its subfolders and files already all have permissions of 755.
My Base Url is set to
http://xxxxxxxxxxx/~test/
My .htaccess looks like this:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt|static)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
EDIT: If it makes a difference, I should say that I modified the core router.php (I know it's better to create my own MY_router.php, but anyway) to allow hyphens in the URL:
$segments = $this->_validate_request(str_replace('-','_',$segments));
but I tried changing it back to
$segments = $this->_validate_request($segments);
and it made no difference.
EDIT 2:
Another update. It works with URLs like this:
http://xxxxxxxxxx/~test/about-us/consultancy
just not with this
http://xxxxxxxxxx/~test/about-us
although this does work:
http://xxxxxxxxxx/~test/about-us/

Codeigniter in localhost WAMP Windows

I try to use Codeigniter in localhost with WAMP.
Problem is, that I can open just basic function in basic controller, and when I tried to use root like site/controller/function, its do not work. Result is that it put me back to wamp start page, or text that page not found.
Mostly I found solutions about makingthe .htaccess file, and many variations what to put in it.
To uncomment rewrtie mod in wamp .conf file. And in my web page to leave index_page empty.
But It do not work for me.
My controller:
class Home extends CI_Controller {
function index() {
$this->load->view('index');
}
function slide() {
$this->load->view('slider');
}
}
And my web page is in: www/mysite/....
Try this .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|indexcp\.php?|resources|robots\.txt)
RewriteRule ^([A-Za-z0-9_/.-]+)$ index.php?$1
RewriteRule ^(CSI) index.php?/CSI
AddType text/x-component .htc
EDIT
Add the above code here
Don't change any thing in CI system..
Once check this repo

Codeigniter: how to serve static html pages in codeigniter context

Hi all
I want to serve some static html pages in codeigniter context. I dont want to bypass the index.php file in the base_url.
But when I user the call to the HTML file, it dispays the 404 error page.
I appreciate any help for this topic from guys who already solving this problem . Thanks for all in advance
I want to precise this elements:
my real problem is to serve PDF file for users to display in the browser only an not to save the PDF file. So I convert the PDF to HTML Files. The output is a directory with HTML files linked betwen them and ther images/button, CSS, js for navigation
when i put this HTML-OUTPUT in ci\Docs\HTML1 directory I can access it whith this URL localhost/ci/docs/html1/index.htm.
But where i use localhost/ci/index.php/docs/html1/index.htm I get A 404 error page.
So I moved The HMLT1 directory to the Application\views directory. And i stil have the same error. My .htacces look like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Thanks Guys for your help
Create the static HTML file as a view and then just load the view:
$this->load->view('static.html');
You can then access this file as a normal controller. It all depends on how you have set up your .htaccess as well.
For me I just add a folder name for my static html pages into the .htaccess to exclude it from regexp like below, and it work. Just to exclude it from rewrite URL to index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|sitemap\.xml|static-html)
RewriteRule ^(.*)$ index.php/$1 [L]
$route['your-page-url'] = "/page/message/your-page-url";
In your Page controller function:
public function message($page = 'your-page-url')
{
$data['title']= 'Your Page Title';
$this->load->view('/header',$data);
$this->load->view('pages/your-page-url');
$this->load->view('/footer');
}

Resources