htaccess help in coexisting Codeigniter and Wordpr

2019-02-20 11:18发布

问题:

My intention here is to have a codeigniter app with a blog under the same domain. The admin for both should be separate.

The directory structure is this:

httpdocs/.htaccess
httpdocs/application/ <== CI installation
httpdocs/blog/        <== WP installation
httpdocs/blog/.htaccess

URLs would be like this:

mysite.com         <== CI
mysite.com/tools   <== CI
mysite.com/forum   <== CI
mysite.com/blog    <== WP

I followed this tutorial to set up and it worked nicely.

Both my codeigniter app and WP blog home page show exactly as they should, with correct URLs, styles etc.

I can also access mysite.com/blog/wp-admin normally for WP back end stuff.

My issue is when I try to access an actual blog post, for example:

mysite.com/blog/fiction/what-is-going-on/

When I do this, I get Codeigniter's 404 error page.

My root .htaccess (httpdocs/.htaccess) is this:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index\.php|robots\.txt|blog|assets)
RewriteRule ^(.*)$ index.php/$1 [L]

My blog .htaccess (httpdocs/blog/.htaccess) is this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Any idea what I'm doing wrong? Will post more info if requested. Thanks in advance.

回答1:

OK the solution is to use

CI .htaccess

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

and

WP .htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress

That did the trick. Now all pages show without a problem.



回答2:

I generally use 2 sets of conditions on the CI .htaccess (one on the root folder)

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

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

This will allow the processing of blog URLs first and then deal with the CI URLs. You need to be careful not to have any controllers or routes using the term blog in your application.



回答3:

I would try three things in your Wordpress .htaccess (don't touch the CI one -- it looks correct):

First, try removing the forward slash from before your index.php:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress

If that doesn't work, then instead, try setting the RewriteBase to /blog/:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress

Finally, if neither of those things work, you can try manually adding "/blog/" to your index.php line at the end:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress