How remove .php extension from codeigniter? [duplicate] - php

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

Related

When coding in PHP how do I get the code to go to the index.php file with different file paths [duplicate]

This question already has answers here:
How to create friendly URL in php?
(8 answers)
URL rewriting with PHP
(5 answers)
Closed 1 year ago.
I am not sure just how to ask this...
www.example.com/
www.example.com/user/list
www.example.com/user/add
How can I make all paths get caught so I can have it go to example.com/index.php and then I can decide what files I want to pull in from the paths...
Instead of doing:
www.example.com/index.php?e=user&a=list
www.example.com/index.php?e=user&a=add
I want something like this:
www.example.com/user/list
www.example.com/user/add
I am not sure if this code will be on a windows server or Unix server. Not sure if I will have .htaccess or not.
you are looking for the .htaccess file !
the .htaccess is a simple file placed in the root directory and allows you to play with the directories and redirect any request to a specific file.
so for example, you can redirect any request for example.com/user/add to example.com/index.php?e=user&a=add
and the user will only type and see example.com/user/add in the address bar. but the actual file that is displayed is example.com/index.php?e=user&a=add
I will take you through the steps needed to achieve that:
in order to display example.com/user/add instead of
example.com/index.php?e=user&a=add first you will need to create a
hidden file which starts with a period and then htaccess and that file should be placed in the main directory
then open that file and write into it the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/add index.php?e=user&a=add [NC]
Note:
but that will redirect the exact same url example.com/user/add. if you wanted the user to be a variable so it could be anything such as example.com/hello/add you will need to change your .htaccess content to this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/add index.php?e=$1&a=add [NC,L]
so know {user} is a variable and will be redirected to index.php?e={user}&a=add

How to write a php code so that I can create a link like this "http://website/2017/name.html" [duplicate]

This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
Closed 5 years ago.
I'm sorry that my question itself is not the way it should be. I am new to php and the links to open a post ( let's take that I am creating a website something like a blog ) is like open.php?id=sth
I have seen in blogger and many other sites the links to open a post is in the type I have given in the question head. I would like to know how this is done. Is this in a way by which they create a directory and make an html file (as in the example) in that particular directory?
You can do that with a ".htaccess" file and apache url rewrite module.You don't need to create a directory.
Create a ".htaccess" file on the root folder and add something like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^?]*) ./open.php?params=$1 [L]
This will make your url like "http://example.com/2017/name"
You can debug your parameters with that code:
var_dump($_GET);
open.php?url=http://example.com
<?php
header('Location: '+urldecode($_GET['url']);
?>

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 Remove .php and route to index.php [duplicate]

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.

building a PHP router [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
turn URL route into funciton arguments php mvc
CMS Routing in MVC
I'm currently trying to rewrite a PHP router.
The new htaccess rewrite has the follows.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/index.php?url=$1 [L]
</IfModule>
Whilst in index.php in public, I am getting the URL using the $url = $_GET['url'];
What I need to do is to pass $url to the Router function:: route($url)
If a URL is passed as : /page/function/$params which would then translate as : index.php?url=page/xapp/function, I'd need to map and route to Controller xapp and call function($params).
By this time, the autoloader has already been called. I'd also need to set a default function to be called if only /page/ is called.
How would I achieve this in a router?
You should check out the code of klein.php, a small php router.
I think you should figure it from that solution.
If not, check out also slim here

Categories