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>
If you ever need to restore an old version of the WordPress . htaccess file that you saved, simply deactivate the current . htaccess file, and rename the old, archived file back to . htaccess .
To fix this issue, first change the permission back to a more secure permission using this from the command line. Then change the ownership/group of the . htaccess file to web server user. Now wordpress should be able to right to this file.
Infact these rules are stored in mysql database in the wp_options table under rewrite_rules field.
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
.
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/";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With