19

I have installed WordPress in example.com. I have also installed CodeIgniter in ci folder here example.com/ci/, ci is the folder name , register is the controller name and working URL of CI is example.com/ci/register . My base URL starts with https:// .

Now I have one WordPress URL example.com/hotel, hotel is the page that I have created in WordPress admin, it works fine.

I want to run my CI path like example.com/hotel/ci/register, I think we can do it with some rewrite rule so that my URL would look like example.com/hotel/ci/register. I have added given htaccess for wordpress that redirecting me here example.com/hotel/ci/register. It is showing me 404 error of CI. It means now I am in CI. Now I did following things in routes.php file.

$route['default_controller'] = 'register';
$route['404_override'] = 'register';

Now this URL example.com/hotel/ci/register is working, but this is not right way, next time there will be more controllers then it will not work.

Note: I can not create hotel folder because hotel is a page in the WordPress. If I create hotel folder then WordPress URL example.com/hotel/ will not work. It will redirect WordPress page to the hotel folder. So I have to do it without creating hotel folder. Note example.com=myurl.com .

I need to find another good solution.Any advise or guidance would be greatly appreciated?

Following is my reWrite rule in wordpress htaccess:

# BEGIN WordPress
   <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^/?hotel/ci/register(/.*)?$ /ci/$1 [L]
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
# END WordPress

And following is my CI htaccess:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 
</IfModule>
5
  • Post your ci folder .htaccess
    – Xpleria
    Commented Oct 27, 2017 at 12:54
  • have you changed the base_url in the config.php file to match the new url path?
    – am05mhz
    Commented Nov 2, 2017 at 8:46
  • This is the URL example.com/ci Commented Nov 2, 2017 at 8:58
  • Just a heads up, you should put your custom rule outside of #BEGIN and #END WordPress comments as it might get overwritten by an update.
    – tobiv
    Commented Nov 3, 2017 at 9:06
  • I will do it. Thanks for telling me Commented Nov 3, 2017 at 9:12

4 Answers 4

5
+100

Interesting problem! I set up a Docker container with a fresh install of Wordpress and Codeigniter, I created a hotel page in WP, and a Register controller and view in CI, and got testing. I spent way too long on this, but I did find an answer.

First, your Wordpress .htaccess. As @tobiv pointed out in a comment, you should not add anything between the BEGIN/END WordPress comments as it might get whacked by a WP update. Your redirect has to come before the standard WP rules though, so add it at the top of the file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^hotel/ci/register /ci/register [L]
</IfModule>

# BEGIN WordPress
# ... These are the default, unchnaged WP rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

<IfModule mod_rewrite.c> and RewriteEngine On are duplicated which seems messy but you do need them, as your new rule has to go first so it processes the request before the WP rules do.

You don't need to modify the Codeigniter .htaccess file, the default one is all you need. It should be in the root of your Codeigniter installation, ci/.htaccess:

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

At this point https://example.com/hotel/ci/register will show the Codeigniter 404 page. If you have logging enabled (see config/config.php), and you watch the logfile, you'll see why:

ERROR - 2017-11-14 17:57:20 --> 404 Page Not Found: Hotel/ci

And here's the root of the whole problem. Since the initial redirect is an internal redirect (means the URL shown in the browser does not change), the URI Codeigniter receives to process is the one still shown in the browser address bar - hotel/ci/register. Codeigniter will try to handle a request like that in 2 ways:

  • Look for a matching route in application/config/routes.php

  • Or look for a controller named application/controllers/Hotel.php, with a method called ci;

In our case there is no Hotel controller, and no route to describe how to handle such a request, so boom, 404.

One simple solution is to create a route to handle this request:

$route['hotel/ci/register'] = 'register/index';

And now https://example.com/hotel/ci/register works!

Notes:

  • Setting your default route to register ($route['default_controller'] = 'register';) means that https://example.com/ci/ will also show register. I'm not sure if you want that? You might run into duplicate-content SEO problems if that URL shows the same as https://example.com/hotel/ci/register, so maybe you want something else, or a 404, there.

  • Make sure you remove your $route['404_override'] = 'register'; route;

  • CI base_url is not relevant for this problem, though obviously should be set. Depending on how you want your links to be I think either http://example.com/ci/ or http://example.com/hotel/ci/ would be right.

  • I am not quite sure what the purpose of this condition in your CI .htaccess is for:

    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    

    The existing default conditions already skip files and directories which exist on disk. That's what the !-f and !-d conditions mean - "if the requested pattern does not match a file or directory on disk, then do the redirect".

    If you have a robots.txt file on disk (in your ci/ dir), and someone requests https://example.com/ci/robots.txt, the !-f condition will fail, and the rewrite is skipped - meaning the request is handled without rewrites and robots.txt is returned successfully. Same for index.php. If you have a directory called ci/resources, and someone requests https://example.com/ci/resources, the !-d condition will fail, the redirect is skipped, and the request is successfull.

    I'm not sure about your resources part, but maybe you can remove that condition completely.

  • If you don't need pretty Codeigniter URLs (I mean other than https://example.com/hotel/ci/register, this change won't affect it), and it turns out you don't need that extra condition above, you can get rid of the CI .htaccess completely to simplify things. To do that, just change the Wordpress RewriteRule to the non-pretty version:

    RewriteRule ^hotel/ci/register /ci/index.php/register [L]
    

    And delete your CI .htacces.

1
  • You explained it with better way. I just used Routing. $route['hotel/ci/register'] = 'register'; I have removed index.php from CI. I just used Routing. Means codeigniter URI routing and rewrite rule in htaccess were the main problem in my question. It is solved now. Thanks Commented Nov 15, 2017 at 4:54
2

I think you are on the right path, try this:

# BEGIN WordPress
   <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^/hotel/ci(/.*)?$ /ci/$1 [L]  # remove the register part 
                                              # so that it would be handled by CI
                                              # also remove the "?" in front of hotel
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
# END WordPress

keep your CI htaccess as it is:

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

the last part is match your CI config.php to your new url model

$config['base_url'] = "http://example.com/hotel/ci/";
2
  • I removed register from $route['404_override'] = 'register'; Commented Nov 2, 2017 at 9:05
  • hmmm.. that is quiet weird, I don't see any problem with your htaccess beside the point above, and I don't have a test environment for now, so i can't test it
    – am05mhz
    Commented Nov 2, 2017 at 9:23
2

I don't thing this will be the great answer of your question but if i face the same problem i will use routes see the example code.

$route['(:any)/ci/register']  = "register";

what will the above code do (:any) means any word in first uri and after that you can defined any url you want i defined ci/register you can also do that like this.

$route['(:any)/register']  = "here_you_can_add_your_controller/function";

this will work if hit url like this.

http://www.example.com/any_word_you_want/register

it will hit your controller function. you need to echo something and it will show in your browser.

you can also defined the hotel word in your base url as @am05mhz shows in his answer but i don't thing that's a great idea because in future may you have 2 more words in your url.

Note : the above code example only work if your .htaccess give access of routs as you shows in your question the .htaccess is work for you. For full knowledge of routs please check the documentation of codeigniter URI routing

6
  • I added your $route['(:any)/ci/register'] = "register"; If i remove $route['404_override'] = 'register'; to $route['404_override'] = ''; then it shows 404 of CI. It means your $route['(:any)/ci/register'] = "register"; and mine $route['default_controller'] = 'register'; are same both will show 404 if i dont assign $route['404_override'] = 'register'; Commented Nov 13, 2017 at 6:38
  • in your code when index.php undefined the root url then it's showing 404 and you set your controller as 404 error. yes it is in your routs but it's only catch the error routs. if you want to achieve this you need place codeigniter in this path public_html --> hotel --> and_here_your_codeigniter then it's will work otherwise you can't do that. i'm not sure about your .htaccess if it's give to access your controller then the above routs 100% work for you. Commented Nov 13, 2017 at 6:47
  • if you place codeigniter in your public html folder then also the above routs will work for you. Commented Nov 13, 2017 at 6:54
  • I am sorry I can not put my codeigniter to this "hotel" folder because it is page in wordpress. are you saying the same? Commented Nov 13, 2017 at 7:08
  • you create a new folder in public html if you only want to see hotel in url if you are mixing WordPress and codeigineter then it's not possible Commented Nov 13, 2017 at 7:29
1

Use htaccess [P] flag instead.

Make sure the substitute (target) string will start with http://. Otherwise, it will treat the substitute string as an internal file path.

Check the code below.

# BEGIN WordPress
   <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteRule ^/?hotel/ci/(.*)?$ http://example.com/ci/$1 [P]

    # If you want rewrite any URI as long it has */ci/* in it. 
    # Use the following rule instead.
    # RewriteRule ^(.*)/ci/(.*)?$ http://example.com/ci/$1 [P]

    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
# END WordPress

Also, you don't need to update CI's base url. Empty string will do.

$config['base_url'] = '';

Hope that helps.

Links:
https://httpd.apache.org/docs/current/rewrite/flags.html#flag_p

2
  • Showing me following error: Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at [email protected] to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request. Commented Nov 6, 2017 at 5:07
  • I see... It's a "SSL Proxy" issue. Add SSLProxyEngine on inside your <Virtualhost> that will fix. If not, add these directives (visit the link): stackoverflow.com/a/22069646/959060
    – paolooo
    Commented Nov 6, 2017 at 17:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.