douxi8759 2016-09-13 08:57
浏览 69
已采纳

htaccess旧文件与.php新文件没有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

  • 写回答

2条回答 默认 最新

  • dt2015 2016-09-13 09:13
    关注

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

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^/?([\w\/_\.-]*)\.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 ^/?([\w\/_\.-]*)\.php$ /$1 [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画