duannian3494 2015-03-13 17:56
浏览 70

如何做.htaccess URL重写以包含URL中的index.php?

I have a php-based system (called OJS) installed on my host. It uses SMARTY to create pages. I have pages with URLs such as: http://example.com/ojs/index.php/foo

I want to be able to type http://example.com/ojs/foo in the address bar, and .htaccess would add the index.php into the url, and execute the new full URL so that the index.php can generate the required page.

Now I use the following in the .htaccess file in ojs folder.

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
   RewriteEngine on
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.*)$ index.php/$1 [QSA,L]
  </IfModule>

I have also tried so many other syntax and directives in the .htaccess. but non of them works. When I enter http://example.com/ojs/index.php/foo in address bar I get the page I want. When I enter http://example.com/ojs/foo (or any page not defined in ojs such as http://example.com/ojs/blahbla or http://example.com/ojs/index.php/blahblahbla) it shows the main page http://example.com/ojs/

I did a test and I guess Rewrite rule at least is doing something. I made a dummy php code in file index2.php (in ojs folder), which echos the superglobal values. For this test I modified the rewrite rule to include index2.php, and I get following results:

$_SERVER['HTTP_HOST']: example.com
$_SERVER['REQUEST_URI']: /ojs/anything
$_SERVER[SCRIPT_NAME]: /ojs/index2.php
$_SERVER['PHP_SELF']: /ojs/index2.php
$_SERVER['REQUEST_METHOD']: GET
$_SERVER['PATH_INFO']:  
$_SERVER['ORIG_PATH_INFO']: /anything
__FILE__: /home/user/public_html/ojs/index2.php  

Can anyone think of why it works with index2.php and it does not work with index.php ?

My configuration if it helps:

PHP version 5.3.29, Apache version 2.2.29, host: Cpanel, Shared Linux host,
I do not have access to the main error log or Apache config file.

Thank you very much,

  • 写回答

2条回答 默认 最新

  • duanguoyin7008 2015-03-13 18:05
    关注

    I Think this is similar to CodeIgniter where we remove index.php from url. You can use below .htaccess code to remove index.php from url. You must have to put below code to your root folder of project.

    RewriteEngine on
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond $1 !^(index\.php)
    RewriteRule ^(.*)$ index.php/$1 [L]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题