Codeigniter web.config or .htaccess index.php rewr

2019-06-19 01:42发布

问题:

my website folder structure is like below...

Application Root
|- administration
|--|- application
|--|- system
|--|- index.php
|--|- web.config
|
|- application
|- system
|- index.php
|- web.config

Two CI framework installed here.

  1. site root
  2. administration folder

Here I am talking about the administration subfolder.

My web.config URL Rewrite is like below.

<rules>
         <rule name="Rewrite to index.php">
            <match url="index.php|robots.txt|images|test.php" />
            <action type="None" />
        </rule>
        <rule name="Rewrite CI Index">
            <match url=".*" />
            <conditions>
                <add input="{REQUEST_FILENAME}" pattern="css|js|jpg|jpeg|png|gif|ico|htm|html" negate="true" />
            </conditions>
            <action type="Rewrite" url="index.php/{R:0}" />
        </rule>
    </rules>

problem is, if I remove index.php from $config['index_page'] of site_root/administration/application/config/config.php, any controller function shows 404 page not found. The site root web.config actually doing this.

I want to remove index.php from URL inside the subdirectory.

1. How could I perform it through web.config ?

2. How could I perform it through .htaccess ?

I found THIS, but got no answer.

回答1:

I've just run a mockup as per your folder structure on a Linux System.

I added the standard htaccess file as you find in the codeigniter users guide. So there are two .htaccess files. One in the main application (root) Folder and the other under the administration folder.

Removed the index.php from $config['index_file'] ='' in both config files.

Created a Home controller in both with a whoami method that just echos back "I am the administration home controller page" for the administration Home controller called by /administration/home/whoami and "I am the main home controller" for the /home/whoami.

Both returned their appropriate messages indicating that each is being called correctly.

The .htaccess straight out of the users guide.

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

So I imagine the same should work with IIS but dont hold me to that. I found something that may help there... web.config version of the .htaccess

So for the above setup you should be able to put in the appropriate urls to hit both CI Instances.



回答2:

will you try add administration rule before CI Index section as follow:

    <rule name="Rewrite Administration Index">
        <match url="administration/(.*)" />
        <conditions>
            <add input="{REQUEST_FILENAME}" pattern="css|js|jpg|jpeg|png|gif|ico|htm|html" negate="true" />
        </conditions>
        <action type="Rewrite" url="administration/index.php/{R:1}" />
    </rule>
    <rule name="Rewrite CI Index">
        <match url=".*" />
        <conditions>
            <add input="{REQUEST_FILENAME}" pattern="css|js|jpg|jpeg|png|gif|ico|htm|html" negate="true" />
        </conditions>
        <action type="Rewrite" url="index.php/{R:0}" />
    </rule>


回答3:

I had the same problem but it's working now. Here's what worked for me. Fu Xu's answer was good, but it didn't quite work. So, if you make the following modifications to his code, it may work for you as it did for me.
I changed
<rule name="Rewrite Administration Index">
to
<rule name="Rewrite Administration Index" stopProcessing="true">

then I changed
<match url="administration/(.*)" />
to
<match url="^administrator/(.*)$" ignoreCase="true"/>

then
<conditions>
to
<conditions logicalGrouping="MatchAll">

and also modified this
<add input="{REQUEST_FILENAME}" pattern="css|js|jpg|jpeg|png|gif|ico|htm|html" negate="true" />
to
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />

here's the full code:

<rule name="Rewrite Administration Index" stopProcessing="true">
  <match url="^administration/(.*)$" ignoreCase="true"/>
  <conditions logicalGrouping="MatchAll">
     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>
  <action type="Rewrite" url="administration/index.php/{R:1}" />
</rule>

<rule name="Rewrite CI Index" stopProcessing="true">
  <match url="^(.*)$" ignoreCase="true" />
  <conditions logicalGrouping="MatchAll">
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>

  <action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" />
</rule>

I hope this works for you



回答4:

Edit in htaccess file...which will remove index.php from your url.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /projectname
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>


回答5:

1) create a file name= .htaccess in application root. add the following code in .htaccess file.

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]

2) goto application->config->config.php.

replace $config['uri_protocol'] = 'REQUEST_URI';

with this one

$config['uri_protocol'] = 'AUTO';


回答6:

    1.Go to **"application/config/config.php"** then Edit
     $config['index_page'] = "index.php" 
     To 
     $config['index_page'] = ""
     AND
     $config['uri_protocol'] ="AUTO"
     to
     $config['uri_protocol'] = "REQUEST_URI"

    2.Now go to Root directory not Inside */application* then create file with file name .htaccess and paste the bellow code and save the file.

    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

    3.Now you have to enable your server rewrite_mode/rewrite_module, so search for it in google For your LAMP/WAMP server...

    4.restart your server

You nailed it bro, Now everything is done...