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
.