Installing wordpress in sub-directory with another app's .htaccess in root directory

So I have a .htaccess at the root directory of example.com:

DirectoryIndex index.html index.php

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]

</IfModule>

Now I’m installing wordpress at example.com/blog/, but after uploading wordpress files there, it’s Page Not Found 404 error when I visit http://www.example.com/blog/

My take is I should modify the .htaccess in the root directory of example.com or the request is redirected to /index.php rather than /blog/index.php

This is the line I added into the .htaccess in the root directory:

RewriteCond %{REQUEST_URI} !^/blog

So the whole thing looks like:

DirectoryIndex index.html index.php

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_URI} !^/blog
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]

</IfModule>

But it doesn’t work. It’s still 404 error at example.com/blog/. I thought it should be something like that, right?

Then how should I modify the .htaccess in the root directory to make this work? Thanks a lot!

yy,

A few comments about your code:

[rant #4][indent]The definition of an idiot is someone who repeatedly does the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> … </IfModule> wrapper is the same thing in the webmaster world. DON’T BE AN IDIOT! If you don’t know whether a module is enabled, run the test ONCE then REMOVE the wrapper as it is EXTREMELY wasteful of Apache’s resources (and should NEVER be allowed on a shared server).[/indent][/rant 4]

Okay, nothing personal, just a pet peeve as you’re abusing the server (because you were not aware what the <IfModule> wrapper does).

Second, I don’t like the use of RewriteBase because it’s designed to UNDO a mod_alias redirection (and you have none in your code). Since it does nothing, it’s also an abuse of the server.

Third, the remainder is typical WP code EXCEPT the file extension exclusion (and sef_rewrite key). However, because all 404’s are handled by index.php, why not allow it to be the 404 handler (especially because you don’t show that you’ve provided one)?

Just what does the sef_rewrite key do for you? Is it intended to duplicate Apache’s {IS_SUBREQ} variable? Why do that?

Finally, if you’ve installed WP in the blog subdirectory, why not inform WP of that simple fact so it can handle the offset correctly?

Regards,

DK

Hi dklynn, thanks for your insights, they are very helpful! I will get rid of the ifmodule eveil…

DirectoryIndex index.html index.php

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_URI} !^/blog
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]

</IfModule>

is the default .htaccess for cs-cart (a php shopping cart software by cs-cart.com) which I installed on the root domain at example.com.

Now I want to install wordpress at example.com/blog, so I created a sub-directory ‘blog’ and when I access http://www.example.com/blog/ which is working perfectly, showing the typical Apache directory files index because it’s empty.

However, after I uploaded all the wordpress files there, and access it again at http://www.example.com/blog/, it’s a 404 error page of cs-cart (which is installed at example.com). I couldn’t even install wordpress.

My take is I should somehow exclude /blog/ from http://www.example.com/.htaccess for the rewrite but pass all control to that of wordpress? However I have no idea how to do that…

Thanks a lot!

yy,

First, thank you for not taking offense at my Standard Rant #4! Moreover, it’s good that you now recognize that <IfModule> is evil (when in the .htaccess file, it has to be executed multiple times for every file request and that is a terrible waste of server resources. Personally, any webmaster who allows an <IfModule> to remain in an .htaccess file MUST be removed from any shared server as he/she is clearly not qualified to be a webmaster.

Please note that creators of shopping carts, etc., MUST include the <IfModule> tests to prevent disabling the websites of “clueless” web(masters|dummies) because they don’t know whether the module is installed or not (and have no clue how to determine whether it is or not). Basically, they don’t want to be yelled at by people who have no business uploading code, i.e., their use is justified.

DirectoryIndex index.html index.php

[COLOR="#FF0000"]<IfModule mod_rewrite.c>[/COLOR]
RewriteEngine on
[COLOR="#FF0000"]RewriteBase / # what is this doing for you?
RewriteCond %{REQUEST_FILENAME} !\\.(png|gif|ico|swf|jpe?g|js|css)$ # what is this doing for you?[/COLOR]
[COLOR="#0000FF"]# escapes the blog directory IF the leading / is removed (Apache 2.x server)[/COLOR]
RewriteCond %{REQUEST_URI} !^[COLOR="#FF0000"]/[/COLOR]blog
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]

[COLOR="#FF0000"]</IfModule>[/COLOR]

I believe your problem is with the !^/blog regex because Apache 2 will fail to match ^/blog (because of the /) so it will redirect to index.php with your sef_rewrite=1 query string.

As an alternate solution, though, I had a client which needed both WP and (I don’t remember which the other program was) so I built a home page which offered links to both in first level subdirectories where the mod_rewrite could be specific to each application.

Regards,

DK

Sorry, I mistyped the code. It’s this code snippet that’s originally in the .htaccess of the root directory of example.com where I installed cs-cart:

DirectoryIndex index.html index.php

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]

</IfModule>

This line of code:

RewriteCond %{REQUEST_URI} !^/blog

Was what I added there because I thought it would solve the problem. But apparently it didn’t. It still kept giving the 404 error when I access http://www.example.com/blog/ where all the wordpress files are uploaded and ready to be installed.

Hey Dklynn, turns out it’s a problem of some file permissions error on my server that is causing the problem. Now everything’s working perfectly.

Your advice is a great one and I thought I should tell the world about it. I just published a post about this and as a thank-you, linked to you. :wink:

yy,

Thanks, Kavoir, that was very kind of you!

However, please don’t forget that ^/ needs special treatment:

Apache 1.x => ^/
Apache 2.x => ^
Apache 1.x and Apache 2.x => ^/?

If you don’t use the correct (or generic) version, you will have unexpected results from mis-matches because the two versions of Apache treat the leading / differently (albeit there are few Apache 1.x servers “in the wild” so the old ^/ is nearly a thing of the past).

Regards,

DK

Hi DK,
Just came across this via Google and wanted to clarify;
Should we remove <IfModule mod_rewrite.c> from htaccess on all our WP sites to improve performance?

Maybe WP devs need to be made aware?
Thanks,

  • Vince

Vince,

WP is aware of the problem but from the other angle. They are using the <IfModule> wrapper to protect THEMSELVES from ID-ten-Ts who use code they don’t understand. Think about it. If an ID-ten-T does not have mod_rewrite enabled and installs WP with the mod_rewrite code, that code will cause the server to go into fits as it would not recognize the code and declare it to be in error. That would cause ID-ten-T’s websites to report endless 500 errors. To protect themselves, they’ve included this “innoculation” code to prevent the mod_rewrite from being reported as an error (and for them being blamed for the IS-ten-Ts’ stupidity).

From a webmaster’s point of view, FROM A HOST’S POINT OF VIEW, repeatedly testing that a module is enabled for every request is a horrible waste of Apache’s resources. Would you want to be on a shared server with one of these ID-ten-Ts? I believe that a webmaster MUST understand the code he/she uses. In other words, I accept no excuses from the ID-ten-T’s masquerading as webmasters (at least once they understand the problem). Yes, I am hardover on this (it’s a personal responsibility issue); I just have a low tolerance for stupidity.

Good question … although I’ve pointed this out repeatedly in the past. Thanks for allowing me to “go off” about this in more detail.

Regards,

DK