Forum Moderators: phranque

Message Too Old, No Replies

Complex URL rewriting problem

Need to rewrite part of the URI in a special case

         

kaspar

2:36 pm on Aug 25, 2005 (gmt 0)

10+ Year Member



Hi Justin (and all other experts in mod_rewrite!),

I need to implement a special case in httpd.conf.

Here's what I have now:

ServerAlias *.domain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^blog\.domain\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$
RewriteCond ^www\.domain\.com/member/%2!-d
RewriteRule ^(.*)$ /member/%2/$1 [L]

This works fine for allowing subdomains to work as:
[subdomain.domain.com...] which is rewritten as:
[domain.com...]

But I also need to rewrite:
[subdomain.domain.com...] OR
[subdomain.domain.com...]

to:

[domain.com...]

What do I need to change and/or add to make this happen?

Also, do I need to keep the final $1 in this line:

RewriteRule ^(.*)$ /member/%2/$1 [L

or change it so it doesn't prevent the last substitution?

Everything I've tried so far has either given me a 404 on

[subdomain.domain.com...] OR
[subdomain.domain.com...]

or else locked up Apache so it wouldn't restart!

Thanks in advance,
Frustrated

jdMorgan

9:16 pm on Aug 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This line is incorrect:

RewriteCond ^www\.domain\.com/member/%2 !-d

and should be either:

RewriteCond %{REQUEST_FILENAME}/member/%2 !-d

-or-
 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}/member/%2 !-d

You will have to test to determine which is correct for your server configutration.

I can't comment on your new code, since you didn't post it. Please review and post any pertinent data from your server error log. It will often tell you exactly what's wrong.

Jim

kaspar

11:29 pm on Aug 25, 2005 (gmt 0)

10+ Year Member



Thanks Jim,

When I replaced:
RewriteCond ^www\.domain\.com/member/%2!-d

with:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}/member/%2!-d

and then with:
RewriteCond %{REQUEST_FILENAME}/member/%2!-d

there was no noticeable change in system behavior.
Everything that was woring before continued to work.

.
.
.
RewriteCond %{REQUEST_FILENAME}/member/%2!-d
RewriteRule ^(.*)$ /podcast/%2/$1 [L]

Here's my latest attempt at a rewrite rule:

RewriteRule ^/load\.mp3(.*)$ /load.php?f=$1 [L]

I just get a 404 error when I try to visit:
[subdomain.domain.com...]

The error log is empty. Literally. Zero bytes. But the access_log is huge (431MB). I tried to tail it from a command shell, but Webmin just comes back with "Page cannot be displayed". It does that with a lot of shell commands...

I suspect any error message are in the access_log. (?)

Does my last Rewrite Rule even make sense?

kaspar

11:31 pm on Aug 25, 2005 (gmt 0)

10+ Year Member



Ooops!

This:
.
.
RewriteCond %{REQUEST_FILENAME}/member/%2!-d
RewriteRule ^(.*)$ /podcast/%2/$1 [L]

Should read:
.
.
.
RewriteCond %{REQUEST_FILENAME}/member/%2!-d
RewriteRule ^(.*)$ /member/%2/$1 [L]

in my last post.

jdMorgan

2:07 am on Aug 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteRule ^/load\.mp3(.*)$ /load.php?f=$1 [L]

I just get a 404 error when I try to visit:
[subdomain.domain.com...]

Is this code in httpd.conf or .htaccess? If in .htaccess, then remove the leading slash from the beginning of the RewriteRule pattern, as it is stripped from the local URL-path and will not be present. Therefore, a Rule in .htaccess with an anchored leading slash in the pattern will never match.

I cannot tell you which of the two variants of the "!-d" RewriteCond I posted will be correct for your server, only that the RewriteCond you had was wrong and won't work, as it's trying to use a canonical URL as a directory name, and see it exists as a directory. Directories, as a rule, don't ever start with "http://"

Terse, big hurry, please forgive typos, apologies... Gotta go.
Jim

kaspar

2:25 am on Aug 26, 2005 (gmt 0)

10+ Year Member



Thanks Jim,

The code is in httpd.conf so the anchored slash would be correct. I'll work on implementing your other suggestions.

I appreciate your help.

Kaspar

jdMorgan

4:53 am on Aug 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it's in httpd.conf, then you'll need anchored slashes in your other patterns as well. Otherwise, you'll end up with double-slashes in the rewritten URL. Actually, you can do either:

RewriteRule ^/(.*) /new-path/$1

or

RewriteRule (.*) /new-path$1

In the first case, we don't put the slash in the $1 back-reference. Instead, we add a slash manually, before the $1 in the substitution (new) URL-path.

In the second case, we do copy the leading slash into $1, and then use it in the substitution, so there's no need to add one there manually.

Which one you choose is largely a matter of style; I use the first form because I find it easier to remember to change them when I copy httpd.conf code to .htaccess, or vice-versa. But the point is to avoid adding an extra slash to the substitution URL, so either way will work.

Jim