Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.


Solution: 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.

share|improve this question
1  
It's great that you found the answer, but to stay in the Q&A format of the site it would be better if you actually posted it as an answer instead of appending it to your question. –  Jan Fabry Jan 9 '11 at 13:33
1  
I thought about doing that, but figured that since the answer I selected led me to what I implemented, it was better to give sanchothefat the points and just include the specifics here. –  ggutenberg Jan 10 '11 at 6:51
add comment

4 Answers

up vote 1 down vote accepted

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:

http://example.com/labs/project1/

Will really be looking here:

http://example.com/labs-folder/project1/
share|improve this answer
    
This looks very promising. I'll test it tomorrow. Any chance you could explain what you're doing in the .htaccess so that I'm not just blindly copy/pasting? I'd like to learn something as well (I know, I'm asking a lot) :) –  ggutenberg Jan 7 '11 at 21:24
    
Turns out WP generates 301s all over the place, and my browser was caching them causing testing to be a pain. I've updated my question with the full solution I used based on this answer. Thanks :) –  ggutenberg Jan 8 '11 at 22:10
    
Hi, by way of a simple explanation - the RewriteCond line tells apache/IIS to start the rewrite engine if there is a URL beginning with '/labs/' and with one or more of any character after it. The rewrite rules and conditions use Regular Expressions to make matches and substitutions. The RewriteRule which is activated if the condition is met causes the server to capture any characters in the url after '/labs/' and map them to the alternative folder. In short you need some knowledge of regular expressions and some basic apache/IIS knowledge. –  sanchothefat Jan 10 '11 at 10:19
add comment

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.

share|improve this answer
    
Sorry, I wasn't clear about this in the original question. WP is installed in the root, so /labs/ is just a page. But it also has to be a directory so that the projects will have /labs/project1 as part of their URL when navigated to. –  ggutenberg Jan 6 '11 at 9:49
    
sorry, I don't have a clue what you are trying to accomplish –  Piet Jan 6 '11 at 15:25
add comment

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.

share|improve this answer
    
Any suggestions on how that .htaccess trickery might go? I'm pretty poor with .htaccess syntax. –  ggutenberg Jan 6 '11 at 21:38
add comment

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]
</IfModule>

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 ...)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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