Wordpress and CI rewrite rule in htaccess Wordpress and CI rewrite rule in htaccess codeigniter codeigniter

Wordpress and CI rewrite rule in htaccess


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 OnRewriteRule ^hotel/ci/register /ci/register [L]</IfModule># BEGIN WordPress# ... These are the default, unchnaged WP rules<IfModule mod_rewrite.c>RewriteEngine OnRewriteBase /RewriteRule ^index\.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /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 OnRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ 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 onRewriteCond $1 !^(index\.php|resources|robots\.txt)RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ 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/";


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