daulat daulat - 2 months ago 13
Apache Configuration Question

htaccess Old files with .php new files without php

Hello everyone my problem is ..

I'm working on codeigniter i'm redirecting all my urls in one controller home

Using this code in application/config/routes.php

$route['(.*)'] = "Home/index";


And any other files that i want run with different controllers

I do something like this

$route['test/get']="test/get";
$route['test/index']="test/index";


And at the end of the route file i add this line

$route['(.*)'] = "Home/index";


Inside of home controller i handle all urls ....

Everything is working fine

My .htaccess file look like this

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]


Now my problem is

I have old site http://www.example.com/

All pages are simple php pages like

http://www.example.com/one.php
http://www.example.com/two.php
http://www.example.com/three.php


This code is running without any php framework

Now new site with same domain http://www.example.com/

Running on php framework codeigniter without .php extension

http://www.example.com/one
http://www.example.com/two
http://www.example.com/three


Now all this pages or url is handle by one controller

Now what i want When ever any user search in google or any other search engine and they found link something like

http://www.example.com/one.php


When they click on this link ...

We should redirect them to

http://www.example.com/one


How can we do that

Edit

I have sub route that all sub routes also working from one controller

http://www.example.com/one/x/y
http://www.example.com/two/p
http://www.example.com/three/q/r/s


I want to remove .php from the end of the url

Answer

You can grab the string before .php and then send a 301 redirect:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

This way you tell the search engines to update links they have in their systems to use the new ones without losing any SEO reputation you build.

Edit:

RewriteCond %{REQUEST_FILENAME} \.php$

You need to add this RewriteCond to the file to grab all new CodeIgniter routes. This would result in the following overall .htaccess file.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]