1

I have setup wordpress in the root directory and at the same level I have some other directories e.g named "sq".

Under sq there are subdirectories like first, second, ... under each subdirectory there is index.php file.

Problem is when I am accessing http://example.com/sq/first it is giving me 404 page of root wordpress.

I tried index.html instead of index.php and that is working. So index.php is not woking and I need php not html.

I have tried all the possible google solutions of updating .htaccess but nothing is working for me. e.g I have tried this

# Include in the next line all folders to exclude
RewriteCond %{REQUEST_URI}  !(sq|folder2|folder3) [NC]
RewriteCond %{REQUEST_URI} !^/(sq|mydir/.*)$
RewriteBase /sq/

my complete .htaccess is as

# BEGIN WordPress
<IfModule mod_rewrite.c>
ErrorDocument 401 default
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# Include in the next line all folders to exclude
RewriteCond %{REQUEST_URI}  !(sq|folder2|folder3) [NC]
RewriteCond %{REQUEST_URI} !^/(sq|mydir/.*)$
RewriteBase /sq/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


#----- START DAP -----
RewriteCond %{REQUEST_FILENAME} -f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} (.*)/wp-content/uploads/(.*) 
RewriteCond %{REQUEST_FILENAME} !(.*)(\.php|\.css|\.js|\.jpg|\.gif|\.png|\.txt|\.ico|\.jpeg)$ 
RewriteRule (.*) /dap/client/website/dapclient.php?dapref=%{REQUEST_URI}&plug=wp&%{QUERY_STRING}  [L] 
#----- END DAP -----

</IfModule>

# END WordPress

Please help guys.

Thanks.

  • 1
    What does your complete .htaccess look like? – janh Jan 1 '18 at 8:59
  • @janh I have updated my code – Bhuvnesh Gupta Jan 1 '18 at 9:30
0

All you should need to do is make sure the appropriate DirectoryIndex is set. For example, at the top of your .htaccess file:

DirectoryIndex index.php

This instructs mod_dir to try to serve index.php when requesting a directory, eg. /sq/first/.

If you are requesting URLs/directories that don't already contain the trailing slash, as in your example: /sq/first, then you need to make sure that DirectorySlash On (also part of mod_dir) is also set (although this is the default setting). For example:

DirectoryIndex index.php
DirectorySlash On

However, you should really be requesting the /sq/first/ (with a trailing slash), as otherwise mod_dir issues a 301 redirect in order to append the trailing slash - which is an additional round trip to your server.


RewriteBase /
:
RewriteBase /sq/

Aside: Ideally, you should never include more that one RewriteBase directive in your .htaccess file. Since WordPress is installed in the document root, then RewriteBase should probably be set to the document root, ie. RewriteBase /. If you have more than one RewriteBase directive then the last one wins and controls the entire file. So, in this example, RewriteBase is set to /sq/, not /. However, in your .htaccess file, RewriteBase is not actually being used anyway. (You also have it in the middle of a rule block - which doesn't make much sense - in fact, I'm surprised that doesn't result in an error?)


UPDATE: So, in summary:

DirectoryIndex index.php
DirectorySlash On

ErrorDocument 401 default

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]    

#----- START DAP -----
RewriteCond %{REQUEST_FILENAME} -f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} (.*)/wp-content/uploads/(.*) 
RewriteCond %{REQUEST_FILENAME} !(.*)(\.php|\.css|\.js|\.jpg|\.gif|\.png|\.txt|\.ico|\.jpeg)$ 
RewriteRule (.*) /dap/client/website/dapclient.php?dapref=%{REQUEST_URI}&plug=wp&%{QUERY_STRING}  [L] 
#----- END DAP -----

</IfModule>
# END WordPress

You should avoid adding your own custom code between the # BEGIN WordPress and # END WordPress comment markers as WP itself might overwrite this in a future update.

I removed the section you had to "exclude" certain directories. That shouldn't do any harm, however, it is also unnecessary, given the existing WordPress mod_rewrite directives. WordPress (the mod_rewrite directives above) already excludes physical directories from being routed through WP.

  • thanks for your response but I am not sure where exactly to put your code and what to remove from my current .htaccess. Now in my above .htaccess I have put 2 lines <IfModule mod_rewrite.c> DirectoryIndex index.php DirectorySlash On ErrorDocument 401 default and removed one line RewriteBase /sq/ but not working – Bhuvnesh Gupta Jan 1 '18 at 13:29
  • You should put this code at the very "top of your .htaccess file". The two RewriteCond directives you added to exclude directories should be unnecessary and can be removed. You don't need the <IfModule mod_rewrite.c> wrapper (although leave it if that is part of the WordPress default). You should avoid adding your own code between the # BEGIN WordPress and # END WordPress comment markers since WP oitself could overwrite this. I'll update my answer. – MrWhite Jan 2 '18 at 10:19
  • Thanks for helping me Mr.White but still the code is not working.I am getting 404 when I am visiting example.com/sq/bhuvi9/ . And when I am visiting example.com/sq/bhuvi9/test.html its working, so .php files are not working . Do I need to write any htaccess for subdirectory too? – Bhuvnesh Gupta Jan 3 '18 at 5:04
  • And the 404 is the 404 from the root WP install (as you said in the question), as opposed to an Apache 404 error document? You shouldn't need to do anything else, unless perhaps you already have other .htaccess files in subdirectories that are overriding this?! – MrWhite Jan 3 '18 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.