0

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 Answers 2

1

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]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for reply. I tried. but still it does not work.
Hey there, You need give the read & right permission to .htaccess file.
it has 644 permission.
Can you check by giving 777 once it will start working then you can downgrade the permission to 644
0

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.)

8 Comments

Thanks @The Sidhekin. I will test it. Just a question: Is not it the whole point of URL Rewriting with .htaccess, that when address example.com/ojs/foo is requested from the server, the Apache server first converts it to example.com/ojs/index.php/foo and then processes it. If Apache does it correctly, then the OJS is not supposed to get /ojs/foo, but it should get /ojs/index.php/foo. Am I missing something?
@cybergeek654 REQUEST_URI contains the path component of the original (pre-rewrite) requested URI; the rewrite leaves that alone. The rewrite primarily changes what actually winds up getting served; in this case index.php. It also changes PATH_INFO in your case, and it may optionally change other things besides. (I don't know OJS, but I suspect it uses REQUEST_URI, and I would not be surprised if it completely ignores PATH_INFO, in which case just redirecting to index.php (without the /$1 PATH_INFO) would work the same.)
I see now. Is there a way I can avoid modifying OJS code, and get this working?
@cybergeek654 To change REQUEST_URI? I think that would mean redirecting. Or proxying, perhaps? But I don't think I'd bother with either. Instead, I'd look into OJS configuration, and see if it doesn't allow for this.
OJS has a configuration file in which I can set RESTful URL on, By doing so all the URLS created in the OJS pages will be in the form example.com/ojs/foo and index.php is eliminated from URLS. Also, FAQ of OJS says I should set the .htaccess in my server to do the URL rewriting to include index.php in URLs requested by user. The code they say I should put in .htaccess is exactly the one I pasted in my question. I dunno why it does not work!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.