Codeigniter Remove .php and route to index.php [duplicate] - php

This question already has answers here:
How to remove "index.php" in codeigniter's path
(27 answers)
Closed 9 years ago.
If a user follows this url:
http://localhost/events/info.php
How can I remove the .php extension via htaccess so the user gets routed to index.php and my Events Controller into the Info function? I basically want to create 301 redirects.
http://localhost/events/info
I have old indexed links that I need to redirect to my controller functions so I do not get 404s.

use
RewriteEngine On
RewriteRule ^events/info/$ index.php [L,QSA]

What version of CI you are using?
normally we already got like this without .php
http://localhost/events/info
To route to index.php (in view folder): you only need to do like this in your controller file.
controllers/events.php
function info(){
redirect('events/index');
}
function index()
{
$this->load->view('index.php');
}

A 301 redirect can be achieved using .htaccess
Redirect 301 events/info.php http://localhost/events/info
You may find this tool useful for generating the redirects.
If you just want to route the URL: http://localhost/events/info.php to the info function in your events controller, then you don't have to remove .php from the URL, you can add a route in application/config/routes.php.
$route['events/info.php'] = 'events/info';
If you haven't already then you must also remove index.php, CodeIgniter's user guide explains how to do this.
If you'd like to remove .php from all your URLs to make them look cleaner, then there a lots of resources available, including other answers on SO.

Related

.htaccess redirect to php file [duplicate]

This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
Closed 2 years ago.
because .htacces including the syntax are totally new territory for me, I currently don't know how to create a specific redirect. I have given the following link:
www.example.com/RANDOM_STRING
This link should redirect via .htaccess to a PHP file where RANDOM_STRING is saved into a GET Parameter. Roughly speaking, it should be redirected something like this:
www.example.com/RANDOM_STRING > www.example.com/router.php?link=RANDOM_STRING
Is it even possible to redirect something like this via htaccess?
Thx
To redirect /router.php/?link=RANDOM_STRING to /router/ you can try something like the following near the top of your .htaccess file (using mod_rewrite):
RewriteEngine On
RewriteCond %{QUERY_STRING} ^link=
RewriteRule ^link/$ /link/? [R,L]

CodeIgniter URI routing with "#" as path parameter [duplicate]

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 2 years ago.
I have a Home.php controller and terms method in it. I have a url like this: https://www.example.com/terms and it works fine.
My routes.php:
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['terms'] = 'home/terms';
Now, I need to change the url to be like this: https://www.example.com/#/terms Notice the "#" in-between.
I tried adding this to routes but it didn't work:
$route['#/terms'] = 'home/terms';
The above change show home page when I open https://www.example.com/#/terms
However, if I put any other permitted character $config['permitted_uri_chars'] mentioned in conifg.php in place of # then it works fine:
$route['a/terms'] = 'home/terms'; // this works: https://www.example.com/a/terms
$route['2/terms'] = 'home/terms'; //this works: https://www.example.com/2/terms
I also added # in $config['permitted_uri_chars'] but that too didn't work. Keeps showing home page only.
Also, can anyone explain me why # is not treated the same way as other characters in permitted_uri_chars?
Controller is Home.php and method is terms(). Using CodeIgniter version 3.1.11
Thank You!
EDIT:
I need to know if I can achieve it using Codeigniter's routing? Or maybe some rewrite rule using .htaccess?
This is caused by the web browser, not CodeIgniter. Web browsers treat # and anything after it in a URL to be a reference to an element on the web page pointed by the URL before the # part.
So for a URL https://example.com/#page1, the web browser will make a request to https://example.com/ and then look for an element with ID page1 on the web page.
If you want different pages to load by using a '#' in your URLs, it's done using front-end frameworks like Angular. Check Angular's routing features to understand how it makes the browser load a different view for different URI paths after #.

How remove .php extension from codeigniter? [duplicate]

This question already has answers here:
CodeIgniter removing index.php from url
(35 answers)
Closed 5 years ago.
My link show this:
http://localhost/project/index.php
I want this:
http://localhost/project/
Do not display extension
You don't understand MVC pattern, there is no direct access to files within URL, everything is redirected to one page.
Full URL: http://localhost/project/home/about
root: http://localhost/project
controller: home
method inside given controller: about
Read more about MVC in Codeigniter's website: https://www.codeigniter.com/user_guide/overview/mvc.html
Or here: https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
CodeIgniter provide Routing rules/URI Routing for this.
You can achive this by adding variable to $route array in
/application/config/routes.php file
$old_path = 'home/about'; // no need to add .php to about
$new_path='home/about'; //or about or any other name you want
$route[$new_path] =$old_path;
Now you can visit page by only
http://localhost/project/home/about
For more details :-
https://www.codeigniter.com/user_guide/general/routing.html?highlight=route
Codeigniter's mvc works as project_folder/index.php/controller/function.
If you modify this behaviour with htaccess it might collapse the behaviour of the framework.
Learn how it works and get clarified. This link might help you.
Create a file with the name ".htaccess" just ".htaccess" NOT "test.htaccess" or so.
Write this code in it ->
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Now you can link your sites like this -> Test

PHP how to redirect the page to a different PHP file? [duplicate]

This question already has answers here:
How to create friendly URL in php?
(8 answers)
Closed 6 years ago.
I couldn't find much help while I was looking for my answer that's the reason for the question on here.
Basically I have this bit of code :
More Info</span>
now how is it possible to get what I am passing ?
Do I make a php file called manage.php ? if so what makes it so that it looks at manage.php ? and then get it through $_GET but how is it possible ?
I am not sure how to go about it so I would appreciate any of yours suggestions.
Thanks
Your problem can have two possible solutions:
By creating manage.php file and passing query string to it.
Your code for the link will like this:
More Info</span>
Now on manage.php page, you can access user_id like this:
<?php $user_id = $_GET["user_id"]; ?>
Using .htaccess, create .htaccess file and put the code below in it. .htaccess file must be placed in same dir where your index.php file is.
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L] </IfModule>
Now you can access your query strings on index.php file using $_SERVER['REQUEST_URI'] global variable.
More Info</span>
Your link to access page must look like above note the / after 10000 else user_id will get merge with 10000
Chatting session was performed for the question, view chat transcript here
You need a routing-engine to manage that type of url.
There are many framework that include routing. The most used is Symfony http://symfony.com/doc/current/routing.html
The general syntax would be as follows:
http://domain_name.com/script.php?parameter1=value&parameter2=value
In your case you would have the following assuming that your parameter is
user_id:
More Info</span>
In PHP you would use $_GET['user_id'] to fetch the passed value
$userID = $_GET['user_id'];
By including these lines in .htaccess
RewriteEngine On
RewriteRule ^manage\/([0-9]+)$ manage.php?user_id=$1
You could reach the same page with the following syntax:
http://domainname/manage/2313
If you mean to say that you need to route to a different php file manage.php and get the passed variable from the previous page then either you can modify the link to
"manage.php?val=10000".$user["user_id"]
and then use $_GET to get the passed variable
Or else you can keep the same url use .htaccess to route your request to manage.php?val=$1
or else in frameworks like codeigniter you have routes file where you can define your route.

codeigniter only pages to an existing website

My website structure somewhat looks like below
css/
lib/
js/
index.php
profile.php
products.php
checkout.php
orders.php
invoice.php
I have added a codeigniter folder in there ...
codeigniter/application/
codeigniter/application/controllers/
codeigniter/application/controllers/mycontroller.php
and other files
I can access CodeIgniter stuff by going to mywebsite.com/codeigniter/mycontroller etc fine.
However, I want to get rid of /codeigniter/ part from the URL. So I was wondering if it is possible to create a whitelist of the files which are CodeIgniter specific? For example, if the URL is mywebsite.com/mycontroller then it does CI stuff otherwise it looks for the plain PHP code file. I have only a couple of CI controllers and loads other non-CI files.
Any ideas?
I think you could use .htaccess to rewrite URL's that don't contain .php, css, lib and js. Something like:
RewriteCond %{REQUEST_URI} !^(\.php|css|js|lib)$
RewriteRule (.*) codeigniter/index.php/$1
So:
http://example.com/css/test.css
stays
http://example.com/css/test.css
(as will all requests to css|lib|js. You can append more things here for the rewrite to ignore)
http://example.com/controller/method
becomes
http://example.com/codeigniter/index.php/controller/method
You can test it out here: http://htaccess.madewithlove.be/
More on rewriting: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Short-term Solution
You can start by simply converting the index.php file into a controller and name it whatever you wish:
<?php
class New_default_controller extends CI_Controller {
public function index()
{
// home page stuff here
}
}
Alter the route.php file and set your default controller so that simply visiting your site will trigger the proper controller:
$route['default_controller'] = 'new_default_controller';
Apply the instructions for Removing the index.php file
Now calls to www.mysite.com/profile.php will access the profile.php at your root and calls to www.mysite.com/new_future_page will call your new_future_page controller.
Please let me know if any of this is confusing or you get stuck.
Optimal Solution
I wanted to leave a comment above but this would have been impossible to show as a comment.
You will have to take your PHP files and put them in the controllers folder like this:
codeigniter/application/controllers/profile.php
codeigniter/application/controllers/products.php
codeigniter/application/controllers/checkout.php
codeigniter/application/controllers/orders.php
codeigniter/application/controllers/invoice.php
Please go through and do the Tutorial before continuing any further. Specifically the Static Pages section will help you in achieving your goal.
You will have to convert your current PHP files to follow the flow of CodeIgniter

Categories