Friday, November 3, 2017

Wordpress and CI rewrite rule in htaccess

Leave a Comment

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 .

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.

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> 

1 Answers

Answers 1

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 Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment