Forum Moderators: phranque

Message Too Old, No Replies

Using mod_rewrite to redirect to subdir?

I can't get it to work and don't know how to troubleshoot it.

         

basetwo

5:21 am on Jun 26, 2004 (gmt 0)

10+ Year Member



Hello all....

I've been working on several sites on my WHM/Cpanel server where I'll use fantastico to set up a CMS under a subdirectory of the VirtualHosts document root. (i.e. when installing mambo, I'll install it in /mambo/. Getting to it, then, requires me to type in "http://somedomain.com/mambo/"

I want to set the .htaccess file in the root so that all requests to "http://somedomain.com/" end up going to the subdirectory transparently. After reading several places it looked like what I needed was mod_rewrite, but everything I've tried doesn't work and I just end up with and index listing of my Virtual host root.

I thought this would do it:

RewriteEngine on
RewriteRule ^/$ /mambo/ [R]

I even added "Options -MultiViews" above this working from a clue I saw in another post.

The RewriteRule I got from the apache URL rewriting guide, although it wasn't clear if this would be transparent or act like a typical html redirect. In any case it doesn't work at all.

What am I missing? How do I troubleshoot something like this?

Any help is greatly appreciated.

jdMorgan

2:54 pm on Jun 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



basetwo,

Welcome to WebmasterWorld [webmasterworld.com]!

For use in .htaccess, omit the leading slash on the RewriteRule pattern.

If you want to rewrite requests for any file in root to the same-named file in /mambo, try something like this:


RewriteEngine on
RewriteRule (.*) /mambo/$1 [L]

As written, this is a transparent or silent rewrite within the server filesystem only, no redirection is invoked.

Troubleshooting can be difficult. Check your server error logs if you have problems.

Jim

basetwo

6:31 pm on Jun 26, 2004 (gmt 0)

10+ Year Member



hmm.... well it's still not working.

Specifically, I get a "500 Internal server error" when using the rule as written. I saw in another post that Cpanel does things with rewrite and requires the [PT] flag, so I tried that in place of [L], but no joy.

I also tried it with and without Options -MultiViews in the .htaccess file.

Error logs didn't show anything when I was getting the Internal server error. Maybe I'm looking in the wrong place? Is there any way to turn on a debug or verbose logging mode for apache so I can see what it's choking on?

Thanks for your help,
Brian

P.S. The server is running v1.3 if that makes any difference.

jdMorgan

7:20 pm on Jun 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might want to try adding

Options +FollowSymLinks

ahead of your RewriteEngine on directive. Depending on server configuration, this may or may not be needed. If it *is* needed and is not present, you'll get s 500 error.

You can probably co-exist with Cpanel as long as you always copy the existing .htaccess file from your server before you modify it. Otherwise, changes made with Cpanel will be lost. The real problems with compatibility are between FrontPage Extensions and "manual" .htaccess changes -- that hardly ever works.

If you have server admin privileges, you can enable detailed mod_rewrite logging as described in the mod_rewrite documentation. It's surprising that not even 500 errors show up in your error logs -- I'd suggest contacting your host about that, as it rather leaves you wondering what other errors you might be having and not know about...

You should use [L] even if you do need to use [PT]. Just combine them into [PT,L].

Ref:
Apache mod_rewrite documentation [httpd.apache.org]
Apache URL Rewriting Guide [httpd.apache.org]
Regular Expressions Tutorial [etext.lib.virginia.edu]

Jim

basetwo

10:08 pm on Jun 26, 2004 (gmt 0)

10+ Year Member



More clues

tried FollowSymLinks and still got the error.

turned debugging on in httpd.conf, restarted apache and tailed the output:

Now I'm seeing "maximum number of internal redirects reached." -- using just [L] and [PT,L]

I assume this means a looping error?

As you can tell, I do have root access. It's a "managed" virtual server so I have full control although I didn't do the original setup. I'm trying to do things in .htaccess so I can tell others (who won't have root access) how to do the same thing.

jdMorgan

10:18 pm on Jun 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, I didn't think about that...

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/mambo/
RewriteRule (.*) /mambo/$1 [L]

Jim

basetwo

3:19 am on Jun 27, 2004 (gmt 0)

10+ Year Member



That did it!

Thanks for your help.

The RewriteCond was probably my major problem all along.

So now what I've done is created a symbolic link called main that points to /mambo. My .htaccess file then looks like:

RewriteEngine on
RewriteCond %{REQUEST_URI}!^/main/
RewriteRule (.*) /mambo/$1 [L]

Now I'll create this setup in each Document Root for my hosted websites. Switching to a new "main" website as easy as redoing the symbolic link. So when I want to switch from using Mambo to PHPNuke, I can just create PHPNuke in the subdirectory PHPNuke and relink main to PHPNuke like this:

rm main
ln -s PHPNuke main

and then the new site is in place.

This makes it easy to create a new site that I can work on until I'm happy with it and then swap it in.

basetwo

3:28 am on Jun 27, 2004 (gmt 0)

10+ Year Member



One more thing that I could use help with:

This is my document root:

/mambo -- main website running mambo
/gallery -- a php photo gallery
/main -- symbolic link to main website

Using the above .htaccess, I can't get to /gallery without adding a symbolic link in /mambo to point it back to /gallery (ln -s /gallery /mambo/gallery) And even in doing that the URL ends up looking like "http://mywebsite.com/main/gallery"

Is there a way to modify the RewriteCond so that the RewriteRule won't bother /gallery?

I tried adding the line:
RewriteCond %{REQUEST_URL}!^/gallery/
above the other RewriteCond...

but it didn't have any effect.

jdMorgan

3:36 am on Jun 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use REQUEST U R I not U R L, and make sure you have a space between "}" and "!".
Otherwise, your code should work.

You can also use:


RewriteCond %{REQUEST_URI} !^/(mambo¦gallery)/

to combine the two exclusions. You will have to change the brokenb pipe "¦" character to a solid pipe character before use, though -- posting here modifies them.

Jim

basetwo

12:09 am on Jun 28, 2004 (gmt 0)

10+ Year Member



Heh, it was a typo in my post.. I actually had it that way on the server.

Both methods do work. What I was seeing was that anytime I'd type in mydomain.com/photos/ the URL would change to mydomain.com/main/photos. I found that this behavior went away when I removed the symbolic link /main/photos which pointed back to /photos.

Now the URLs work as advertised.

Thanks!