Redirect the URL to another, in codeigniter - php

I have a webpage which was built with core php, now its been updated to codeigniter mvc. Problem which i am facing is, users are trying to enter the old URL path
example: www.website_name/old_path
now since the mvc layout, it wont work and will get 404 error. i want to redirect the clients who enters www.website_name/old_path for the new one
www.website_name/index.php/new_controller/new_view.
Anyone please help me to resolve this issue.
Thank You!

first of all remove index.php config file.
Open config.php and do following replaces
$config['index_page'] = "index.php"
to
$config['index_page'] = ""
Then create HTACCESS file in application/ folder
having this code....
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Now index.php is removed from your URl....
STEP 2) open application/config/routes.php file
Now here define your desired web URL
$route['mycontroller/(:any)'] = "mycontroller/user_detail/$1";
So,Your URL Will be ..
www.website_name/new_controller(or xyz)/
Here you can also change the controller name differently using routes So no one can get the exact name of controller/method name....To Do such customization refer this URL
http://www.codeigniter.com/user_guide/general/urls.html

As Sumit mentioned, you should use URL Routing for this.
In the routes file you can create a list of all the redirects that you want like so:
$route['old_path'] = "new_controller/new_view";

You can use 301 redirect for this type of issue.
Put this code in .htaccess
RewriteRule ^www.website_name/old_path www.website_name/index.php/new_controller/new_view [L,R=301]
or use 301 redirect of codigniter http://www.codeigniter.com/user_guide/helpers/url_helper.html
// with 301 redirect
redirect('/article/13', 'location', 301);

Related

Hiding controller and method from the URL but show only title name

I looked at the suggested answer(possible duplicate as stakoverflow says). It provided help but didn't solve the problem as it won't help create clean URL as per my needs. It has controller as part of the URL which I don't want to see as mentioned below.
I am developing a tutorial site in Codeigniter, and looking for a way to generate SEO friendly URL like
www.crdlabs.com/how-to-use-multidimensional-arrays
instead of
www.crdlabs.com/home/index/how-to-use-multidimensional-arrays.
Can anyone share his ideas? Thank you
First you need to hide the index.php from URL so create the .htaccess file on the root folder and add this code
.htaccess
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
Then open the routes.php file in the application/config/routes.php location
and add this
$route['how-to-use-multidimensional-arrays'] = 'home/how-to-use-multidimensional-arrays';
This will work as URL as SEO friendly
Add this to ur root .htaccess file :
RewriteRule ^(.*)$ index.php?title=$1 [NC,L,QSA]
Then in ur index.php get the title $title = $_GET['title'] and do what ever you like with it like do a query in ur database and return data based on the title !

Change Particular route in MVC CI

I have below Url in my sample application in PHP MVC CI
http://localhost:1234/Sample/Index.php/User/index
Here User is Controller and index is Action Method
Can I write this url like below ?
http://localhost:1234/Users
After seeing your code , main mistake you have done you have renamed index.php, which is wrong, please revert that index1212.php to index.php
Rest of the thing you can follow other answers.But this is maon, codeigniter will not run without index.php file
Edit
One more error i noticed, in routes.php,
its
$route['Users'] = 'user/';
not $routes
And access your project like this,http://localhost/Sample/Users
And as far as my knowledge, you cannot hide folder name in url.
If you would like to use custom routes. http://www.codeigniter.com/user_guide/general/routing.html
You do not need to include index.php in routes.php
// application > controllers > sample > Users.php filename first letter must be uppercase. Same with class name.
$routes['users'] = 'sample/users/index'; // Lower case preferred.
// When with out function would go straight to index function
$routes['users'] = 'sample/users';
// No Need for index.php
// $routes['Users'] = 'Sample/Index.php/User/index';
Now url will be
http://localhost:1234/project/users
http://localhost/project/users
Or with index.php
http://localhost:1234/project/index.php/users
http://localhost/project/index.php/users
You may need to remove index.php here are some htaccess
https://github.com/riwakawebsitedesigns/htaccess_for_codeigniter
application > config > config.php
$config['index_page'] = '';
htaccess example
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Note WAMP users:
If your using WAMP make sure you have rewrite module enabled.
Sample is your project dir so you can not write the route which you want you can write the route as bellow.
http://localhost:1234/Sample/Users
And your route file will look like bellow.
$route['Users'] = 'User/index';

CodeIgniter Noob - How does the index.php file work?

Here is my views directory:
- views/
- home.php
- contact.php
- assets/
- css/
- main.css
config.php: (changing this doesn't seem to do anything)
$config['index_page'] = 'index.php';
routes.php:
$route['default_controller'] = "welcome";
When I request www.example.com/index.php, the default page (i.e. home.php) loads and all of the assets work. However, when I request www.example.com/index.php/welcome or www.example.com/index.php/welcome/index, the page loads but the assets don't work. The same also happens if I try to load the page from a link from the home page.
I have no idea what index.php is for. I want to be able to just request www.example.com/welcome/index which will call the welcome.php controller and call the index method. application/index.php looks pretty important for everything to work, do I haven't deleted it, but I don't really want it. What can I do?
Thanks.
I think you're asking 3 different questions here.
Firstly index.php is the main entry point for your codeigniter app. It's very important as all of your routes will go through the index. The reason you can change it in the config is so you can rename it to something other than index.php if your setup requires it
Secondly, just guessing here but I think your assets are being loaded using a relative path; prefix all of your assets with base_url(); e.g
<?=base_url();?>assets/css/style.css
Thirdly, you'll need a htaccess file to hide the index.php (to give you www.example.com/welcome), or the equivalent if you're not using an apache server, which will look like the below (taken from http://www.codeigniter.com/user_guide/general/urls.html:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Enable mode Rewriting on your server LAMP/WAMP and add a .htaccess file with following code
RewriteEngine on
RewriteCond $1 !^(index.php|resources)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
after doing above process you can use your urls without index.php i.e like www.example.com/welcome/index as well as your assets directory www.example.com/assets
Before all learn How MVC framework will work,
www.example.com/controller/function
If you call www.example.com , then default controller will load. You can change this in config.php
Inside the function you can call view ( your actual html viewable scripts ). follow the user guide from https://ellislab.com/codeigniter/user-guide
by default codeignitor url will be like www.example.com/index.php/controller/function
index.php can be removed using .htaccess

URL Rewriting in codeigniter rewrite the URL for next page after login

I'm having a hardtime on this part. Well I created a login page using codeigniter wherein the whole path would be http://localhost/CodeIgniter_2.1.3/index.php will be rewritten into http://localhost/login using .htaccess. Well it works just fine but the problem is after I made a successful login and the page redirects to next page using redirect('next_page'); since the URL would be http://localhost/CodeIgniter_2.1.3/index.php/next_page instead of http://localhost/next_page. I also created a rewriterule for that part :
RewriteRule ^events$ /CodeIgniter_2.1.3/index.php/events [L,NC]
and whenever I key in the http://localhost/next_page in the addressbar the page would appear just fine (since I haven't applied any rules on login and on this page.) I also tried using redirect('http://localhost/events'); this also works just fine but still I know this is not the real solution for this. Oh by the way my .htaccess exists on the www folder of wampserver. Any way to solve this?
First of all your domain ROOT folder should be the codeigniter folder
forexample
c:/www/codeigniter_2.1.3/
just move the files from the codeigniter_2.1.3 to your root folder OR make the codeigniter folder ROOT.
Then use just normal .htaccess rewrite as:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
And use the Codeigniter ROUTER to make your pages as you want them
example:
$route['login'] = 'welcome/login';
Will set http://localhost/login page to be loaded from the Welcome controler Login function.
You can set $routes at your routes.php in the config folder.
P.S> If you want not to see index.php on each page check your Config/config.php for the index_page and make it blank.
You might just want to add virtual host to your localhost. here is one step by step explanation on how to do that.
now all you have to do is create an .htaccess file inside your CodeIgniter_2.1.3 folder that will remove only the index.php in the url.

htaccess redirection rules with conditions

I used the following code in my htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule>
It redirects as if the url is test.com/test --> it redirects to test folder (Here the folder is available in the name of 'test')
If the url is test.com/testpage --> it redircts to index.php (i.e no one folder is in the name of testpage)
But i want to redirect this as
if the url is test.com --> it will redircts to index.php
if the url is test.com/test --> It will redirects to corresponding folder (if the folder name exists)
if the url is test.com/testpage then it redirects to page.php
Also i want to redirect some particular name to test.php (test.com/test2 or test.com/test3)
Please help this
This is done by creating a router class.
The code you have there redirects all urls to index.php making index be able to read the url and base it's contect on that.
see http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/
for router tutorial
you can individually write their rewrite conditions for specific pages/urls or just add some sort of suffix like along with page pass "[category]/testpage" or any thing and describe the rewrite condition so that it can be redirected.
RewriteRule ^category/(.*)$ page.php [R=301]

Categories