Welcome Guest, Not a member yet? Register   Sign In
Problem with removing Index.php from URL
#1

Hi Guys

I'm having a problem removing the index.php part in my website's url.

I'm currently migrating a webapplication from 1.7.2 to the new 3.0.4 Version. The Code was a bit outdated and it took quite some time to rewrite some classes and getting it functionning again. But now there is only one last Problem that needs to be solved: Removing Index.php from my URL.

To understand what is not working here a little summary of the Webapplication:
The Application shows all authorized users our companys serversystems. On the left side, one can choose to filter for OS-Versions or Operating Status or even search for specific servernames or IPs. On the right Side is a List, that updates in realtime and shows the matching Systems. If a user clicks on a serversystem in the right list, general Information about the serversystem will be dispalyed in the middle of the Site.

The problem now is, when I change the Option in application/config/config.php from $config['index_page'] = 'index.php'; to $config['index_page'] = ''; my whole site stops working. Either the search nor the filter function are working anymore. The links altough are correct, hiding the index.php page from the url like: https://www.example.ch/system/showItem/123456 which means that my mod_rewrite in the /.htaccess is working correctly (I think so). I also changed the uri_protocol option to REQUEST_URI.

I hope you guys understand my problem and also my english.
I would be glad if someone has a guess that could bring me to a solution.

Thanks in advance
Fuerro
Reply
#2

Try this .htaccess file, also you should upgrade to CI v3.0.6

Code:
<IfModule mod_rewrite.c>

    #Set the CodeIgniter Environment.
    #SetEnv CI_ENV development

       # Make sure directory listing is disabled
    Options +FollowSymLinks -Indexes
    RewriteEngine on

    # NOTICE: If you get a 404 play with combinations of the following commented out lines
    #AllowOverride All
    #RewriteBase /wherever/ci/is
    RewriteBase /

    # Restrict your site to only one domain
    # Important USE ONLY ONE OF THESE OPTIONS BELOW!

    # Option 1: To rewrite "www.domain.com -> domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    #RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    # Option 2: To rewrite "domain.com -> www.domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    #RewriteCond %{HTTP_HOST} (.+)$ [NC]
    #RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

    # Option 3: Remove index.php from URL
    #RewriteCond %{HTTP:X-Requested-With}    !^XMLHttpRequest$
    #RewriteCond %{THE_REQUEST}                ^[^/]*/index\.php [NC]
    #RewriteRule ^index\.php(.*)$            $1 [R=301,NS,L]

    # Send request via index.php (again, not if its a real file or folder)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # deal with php5-cgi first
    <IfModule mod_fcgid.c>
        RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
    </IfModule>

    <IfModule !mod_fcgid.c>

        # for normal Apache installations
        <IfModule mod_php5.c>
            RewriteRule ^(.*)$ index.php/$1 [QSA,L]
        </IfModule>

        # for Apache FCGI installations
        <IfModule !mod_php5.c>
            RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
        </IfModule>

    </IfModule>

</IfModule>
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php/$1 [QSA,L]
</IfModule>




if your site in a subfolder, write the folder name in RewriteBase section. for example if your site is in a folder you must be write the name of folder between // :
RewriteBase /foldername/
Reply
#4

I did the Update and also am using your .htaccess-file now.

It remains unchanged. Thank you anyway!
Reply
#5

(This post was last modified: 06-17-2016, 12:55 PM by behrooz1x. Edit Reason: complete )

You needs three steps:

1-Create htaccess file in the root ci folder(i attach my .htaccess).
2-Change the 'RewriteBase /' in httaccess, for exmaple if your site in url is 'http://localhost/ci/index.php/welcome' you need change that to 'RewriteBase /ci/'.
3-remove 'index.php' from 'application/config/config.php':
//this
$config['index_page'] = 'index.php';
///to
$config['index_page'] = '';

Attached Files
.txt   htaccess.txt (Size: 1.02 KB / Downloads: 93)
Reply
#6

Thank you for your suggestions.

I tried it now with different RewriteBase variants but it doesn't seem to change a thing.

What is unclear for me is, when I remove index.php out of my config.php what happens with all the configurations made in the file? like: $system_path = 'system'; or $application_folder = 'application'; ?

Isn't this equal to deleting this file? CI doesn't use it anymore?
Reply
#7

(This post was last modified: 06-24-2016, 12:16 AM by PaulD.)

No, it is just a config setting and nothing is deleted.

The .htaccess file simply takes the incoming request and reroutes it to the same url, but inserting index.php back into the route, so index.php is still run of course.

Imagine renaming index.php to myfile.php, then the config setting would be 'myfile.php' and CI would know what to write into the urls. If you remove it from the config setting, then the htaccess file would be changed to insert myfile.php into the url route as the request came in. So myfile.php would still be run.

I hope that helps,

Paul.

PS If you remove index.php from config, but do not rewrite the url with htaccess, the url's created will be wrong, and CI will not run because index.php was never called.

PPS To check your .htaccess is even being read, force it to make an error by putting junk in the first line like 'fghdfkghdslkfgh'. If you get a server error, then it is being read. Next make a test.html file to say 'hello world' and try rewriting the url to the test.html. You can then check your .htaccess is read and working. Googling for test my .htaccess gives some good help on this
Reply
#8

(This post was last modified: 06-24-2016, 01:18 AM by Fuerro.)

So it seems that my .htaccess file isn't used by apache.

I know that mod_rewrite is working, because I already tested it once. But I was not thinking about the fact that my file isn't even read by Apache.

That may be the solution, I'm working on it. Thanks!
Reply
#9

Glad you have a clue to work on at least. There is nothing more frustrating than when you just can't even find what to investigate.

Just to note, are you changing the correct htaccess file. The one to do the rewriting URL's in should be in the root of your CI application, not the .htaccess files in the app folders or other CI folders.
Reply
#10

I solved it! First problem was, that I've had "AllowOverride None" active in my Directory.. And then one of my controllers was named "system" like Ci's System-folder which leaded to another problem. Renamed it and now everything is working perfectly and I'm just sitting here after hours of cheking my code facepalming myself.

Thanks alot, would still be checking my code without you guys' help! Awesome!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB