WP Page and Subdirectory with same name

I have a subdirectory within my WP installation called labs. I created a WP page called labs as well. When a user hits mydomain.com/labs/ I want them to load up the WP page. But instead it’s loading up the labs directory listing.

I’ve read a few ways to do this if I want my WP page to have a different name than my directory name, but I don’t really want that. For example, I want to have:

mydomain.com/                <-- WP Root
mydomain.com/labs/           <-- Pretty WP page listing all of my projects
mydomain.com/labs/project1/  <-- raw (i.e. no WP) web app
mydomain.com/labs/project2/  <-- raw (i.e. no WP) web app

Is this possible?

I should also mention that WP is installed in the root of mydomain.com, so /labs/ is both a WP page and a subdirectory that I created manually.

I renamed my labs folder to projects and used the following .htaccess.

RewriteEngine On
RewriteCond %{REQUEST_URI} labs/(.+)$
RewriteCond %{REQUEST_URI} !labs/(.+)/$
RewriteRule ^labs/(.+)$ http://mydomain.com/labs/$1/ [L]
RewriteRule ^labs/(.+)$ projects/$1 [L]

I’m a little unclear as to why I had to use

RewriteRule ^labs/(.+)$ http://mydomain.com/labs/$1/ [L]

instead of just

RewriteRule ^labs/(.+)$ labs/$1/ [L]

But when I used the latter it never appended the trailing slash.

Solutions Collecting From Web of "WP Page and Subdirectory with same name"

You’ll need to do some .htaccess-fu to get what you’re proposing to work.

RewriteCond $1 ^/labs/(.+)
RewriteRule ^/labs/(.*)$ /labs-folder/$1 [L]

This isn’t tested yet but it if you put it before the wordpress rules in your htaccess file it will remap urls that begin with /labs/ to /labs-folder/ but not if the url is just “/labs/” on its own.

Let me know if it doesn’t work and I’ll debug it with you.

You can replace “labs-folder” with any folder name you choose but essentially a url like:


Will really be looking here:


You can’t have both a WP page and a directory with the same name, except if you also create /labs/index.php and do some strange hackery therein.

Another solution would be to rename the directory and do some .htaccess trickery so the files inside the directory are served under the /labs/ URL.

I found (at least on my hosting service) that it is not necessary to rename the conflicting directories. You need to create an exception for each page that you want WordPress to handle, along these lines:

RewriteCond %{REQUEST_FILENAME} /labs/$ [OR]
RewriteCond %{REQUEST_FILENAME} /page2/$ [OR]
RewriteCond %{REQUEST_FILENAME} /page2/page3/$
RewriteRule . /index.php [L]

These rules basically tell the server to pass any requests ending in directories with those names to /index.php which is WordPress’ main script. (This assumes WP is in the root directory). In order to prevent matching any URL ending in /labs/ etc. it is necessary to qualify the conditions with enough of the pathnames to those directories on your server to make them unique. The server path must be used here because I am matching against %{REQUEST_FILENAME}. I suppose %{REQUEST_URI} would allow you match against the URL instead but I haven’t tried it. So each condition becomes something like

RewriteCond %{REQUEST_FILENAME} /path/to/html/labs/$ [OR]

Also, you need to have an [OR] option at the end of every RewriteCond line EXCEPT the last one.

I then inserted these lines before the rewrite rules that WordPress creates that check whether a directory or file exists. Here would be the final set of rules:

# Modified WP rewrites
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} /path/to/html/labs/$ [OR]
RewriteCond %{REQUEST_FILENAME} /path/to/html/page2/$ [OR]
RewriteCond %{REQUEST_FILENAME} /path/to/html/page2/page3/$
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]

Note that I removed the # BEGIN WordPress comment. I then made my .htaccess file non-writable to prevent WordPress from adding its rules back. I don’t think these steps were necessary though. If you put the new rules above the # BEGIN WordPress comment and wrap them in the other lines to turn on the rewrite engine, I think they will still work but I haven’t tested that configuration.

I just migrated my old static HTML site to WordPress and I was trying to keep all of my downloadable files in the original directories with the pages that they belonged to. This setup made it possible.

Hope that this helps someone!

(P.S. Where is the help page for markdown syntax?? I looked for about 15 minutes and could not find it …)

Your question seems to be a few in one 🙂

  1. you should not have to create a page labs as that is the home directory of your WordPress install already.

  2. if the directory listing is showing, then I would make sure that installed WordPress properly. Is the .htaccess file there too and is perhaps the .htaccess file in your main directory (mydomain.com) causing the directory showing in the WordPress directory (mydomain.com/labs/)

  3. With the navigation menu you can set up the links as you want to set them up, so project1 and project2 pointing to things outside of WordPress, but you have to make sure that you don’t give anything within WordPress the same names and I think that you will need to set up subdirectories under the labs directory.

Modify your .htaccess file without renaming your folder:

#a request finished with '/labs/'
RewriteCond %{REQUEST_FILENAME} /labs/?$
#processed by wordpress
#change index.php prefix according to your original wordpress .htacccess
RewriteRule . index.php [L]
#put before the other RewriteConds