0
votes

I want to forward everything to index.php except if no file (REQUESTED_FILENAME) was specified or with other words URI is empty or take one /.

I've tried this conditions to rewrite/redirect all to index.php in the .htaccess - file.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [QSA]

If I call http://my.dom/test.php, the content of the test.php is displayed (!-f).

Next try: Check, if requested file not index.php and then redirect to index.php

RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^(.*)$ /index.php/$1 [QSA,R=301]

Ok this works. But i would like no redirect by empty requested file.

Next try: Redirect if requested file is not empty and requested file is not index.php

RewriteCond %{REQUEST_FILENAME} !^$
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^(.*)$ /index.php/$1 [QSA,R=301]

If I call http://my.dom/test.php it works fine, redirect to index.php/test.php :D.

If I call http://my.dom or http://my.dom/ its also redirect to index.php. But i would like no redirect by empty requested file.

Update (1) I've now this in my .htaccess.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule ^(.*)$ / [QSA,R=301]
#this redirect all files to root

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/$1 [QSA,L]
#this rewrite on server side to index.php

This doesn't work. http://my.dom/test.php was redirect to http://my.dom/. But the second rule makes an internal (550) error (every time redirect). I've put a new condition to the first section

RewriteCond %{REQUEST_FILENAME} -f 
RewriteCond %{REQUEST_URI} !^/.*$
RewriteRule ^(.*)$ / [QSA,R=301]
#this redirect all files to root

By this: http://my.dom/test.php does not redirect.


Please tell me where my mistake is. And maybe a few solutions.

Regards

1
So that you know where you went wrong and to help you understand, you almost had it but you need to use REQUEST_URI for both your conditions, which you said in your first sentence. Also to prevent redirect you need remove the 301 redirect and it be [QSA,L] and this should be this RewriteCond %{REQUEST_URI} !^/$ - Panama Jack
The redirect its OK. If the URL my.dom/file.php - then it should be redirect to my.dom and not rewrite. By rewrite, the URL by the client dosn't change. I've updated my try. - raiserle

1 Answers

0
votes

Its because / is not a file, / points to your root directory. You cant match against your root dir using Request filename variable. To exclude your homepage from the rule, just change your regex pattern to (.+) .

Or you can use the following rule

RewriteEngine on

RewriteRule ((?!index\.php).+) /index.php/$1 [L]