Knowledgebase

Help me with a silly mod_rewrite question

Posted by fog, 01-07-2008, 12:42 AM
I run a multi-user WordPress install. mod_rewrite is in place and works great for what it's supposed to do. However, I've put together a 'custom' front page, and it's in a subdirectory. The current hack of a solution is to have an index.html to intercept requests for / (server root -- the 'normal' main page) and use HTML to send a redirect to /main2, the subdirectory with the main page. This works, but is obviously inelegant. What I'd like to do is add a mod_rewrite rule that redirects requests for / to /main2/ -- this seems like a simple enough task. But for some reason it won't work. I start with... Changing it to "RewriteRule / main2" and so forth doesn't fix anything, either. I initially had [R=301,L] at the end, but took it out to try to get this right. Nothing is appearing in error_log either. Shouldn't this be very straightforward? Edit: the problem I'm seeing is that nothing out of the ordinary happens -- you hit /, get /index.html, and get redirected. In other words, my additions to .htaccess are being ignored. (However, if I use bad syntax, I do get errors, so it's not like an Apache reload is necessary.) Last edited by fog; 01-07-2008 at 12:48 AM.

Posted by foobic, 01-07-2008, 12:58 AM
Not sure about WordpressMU but this works for me with a regular wordpress install in the wordpress subdirectory: If the requested file exists in the top-level directory it's used, otherwise it's passed to wordpress. The normal wordpress SEO-friendly url rules are also included here so it doesn't need another .htaccess in the subdirectory.

Posted by Tim Greer, 01-07-2008, 01:15 AM
The problem is that .htaccess rules will be in effect for all sub directories in that path. If you web host has a structure/hierarchy where you host other domains (addons/pointers) and sub domains in the same path as the primary domain, then it will create this problem. The solution is to add a conditional, so the rules only take effect if it's a request on the domain that uses that main web root. Such as you have: public_html (maindomain.com) public_html/order (order.maindomain.com - sub domain) public_html/anotherdomain.com (addon somain) Any public_html/.htaccess rewrite rules will apply to all three of those (main, sub and addon). You overcome this by adding the conditional for the domain being accessed: RewriteEngine On RewriteCond %{HTTP_HOST} ^(www\.)?maindomain\.com$ [NC] RewriteBase / RewriteRule ^/$ /main2/ That is just an example. This way, it will only take effect if someone accessed the site as www.maindomain.com or maindomain.com (without the leading www.). You can add other options to that list, in case you have sub domains or pointed domains that use the main web root that you want to also have the rules take affect, such as: RewriteEngine On RewriteCond %{HTTP_HOST} ^(www\.|secure\.)?(maindomain|anotherdomain)\.com$ [NC] RewriteBase / RewriteRule ^/$ /main2/ I hope that helps (it should, unless I misunderstood you and your issue).

Posted by fog, 01-07-2008, 01:37 AM
I appreciate the help, but the blogs get their own Apache vhost, so there are no other sites to worry about. Adding the conditions is probably good practice anyway, and I did just add them. However, they don't solve the real problem -- the "RewriteRule ^/% /main2/" line doesn't do anything. It's as if I never added that rule.

Posted by david510, 01-07-2008, 01:58 AM
Using the below rule all request coming to http://www.domain.com/ will redirect to http://domain.com/main2/. ie http://domain.com/anything will redirect to http://domain.com/main2/

Posted by fog, 01-07-2008, 02:35 AM
That wasn't quite what I wanted to do, but it inspired me... I just wanted requests for "http://domain.com/" to end up at domain.com/main2. I think my problem was that the leading slash was being ignored, so a rule matching / never got met. However!does work -- essentially matching NO path. The "L" was necessary because the WordPress rules were then processing it in ways I didn't want. This breaks some things I hadn't thought about. I added another rule to do the CSS file, which was straightforward. But the problem is that the included images aren't working from their new path -- I want requests for domain.com/images/* to be served from domain.com/main2/images/* The rule...Doesn't work, but it seems like it should.

Posted by david510, 01-07-2008, 02:49 AM
domain.com/images/* ---> domain.com/main2/images/*

Posted by fog, 01-07-2008, 02:52 AM
You rock! I was so close with my try, but I guess "close" doesn't mean much when writing regular expressions! This does exactly what I need. Thanks again for the help!

Posted by Tim Greer, 01-07-2008, 01:25 PM
The first attempt you mentioned didn't work because it doesn't resolve to the full URL with the forward slash. I'm glad you got that figured out. Also remember that you can use the ? character for an option.



Was this answer helpful?

Add to Favourites Add to Favourites

Print this Article Print this Article

Also Read
Nac Issues (Views: 576)


Language:

Client Login

Email

Password

Remember Me

Search