简体   繁体   中英

How to do .htaccess URL rewriting to include index.php in the URL?

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,

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]

Frankly I suspect the OJS system is reading REQUEST_URI and expecting it to be of the form /ojs/index.php/foo . It instead gets /ojs/foo , and since it doesn't recognize that, shows the main page.

I guess you need to tell (or modify) OJS to expect the /ojs/foo form.

Just to double check that Apache is not messing up, add a R=301 flag for testing. If I'm right, you should get a redirect to /ojs/index.php/foo – and when the browser follows that redirect, it'll work. Of course, that's suboptimal, so fixing OJS expectations should follow. :)

( QSA is unnecessary here, by the way.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM