Forum Moderators: phranque

Message Too Old, No Replies

redirect help

conditionally insert additional path-info?

         

beuy

11:42 pm on Jul 25, 2010 (gmt 0)

10+ Year Member



Good day there,

I am trying to get a apache redirect working correctly, I had previously had heaps of help on this but the other party is currently too busy to assist.

There are two rules
1. If url request is "features.loc"
than forward to features.loc/bugs
2. If url request is "fetaures.loc/*" (* being anything but bugs/) than add bugs/ i.e
if the request is features.loc/show_bug.cgi?id=13 than rewrite it to features.loc/bugs/show_bug.cgi?id=13

So far I have the following:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^features.loc$
RewriteRule ^(?!bugs[/$])(.+)$ bugs/$1

This handles the second rule really well but I can't seem to get the first one going, any help would be greatly appreciated.

jdMorgan

11:55 pm on Jul 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"features.loc" is certainly not a hostname (i.e. a domain)... So that RewriteCond won't work.

Give this a go:

RewriteCond $1 !^/bugs
RewriteRule ^features\.loc(/.*)?$ features/bugs$1 [L]

Jim

beuy

12:05 am on Jul 26, 2010 (gmt 0)

10+ Year Member



Hi there jd,

Sorry I should have explained the environment a bit more, features.loc is a host alias setup in the local domain to point to an internal server. This server is internal to the network and not accessed from the outside world.

Additionally there is another host alias of wsrwiki that points to the machine as well.

jdMorgan

1:01 am on Jul 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, so it is a hostname then... :o

Because of the form of your code that you posted, I assume that this code goes into /.htaccess. If not, then it will need to be changed bay adding a leading slash to the RewriteRule patterns and the 'loop prevention' measures won't be needed.

Rewriting to either /bugs (with no trailing slash) or /bugs/original-path-info complicates things. Straightforward two-rule solution:

RewriteCond %{HTTP_HOST} ^features\.loc\.?(:[0-9]+)?$
RewriteRule ^$ bugs [L]
#
RewriteCond %{HTTP_HOST} ^features\.loc\.?(:[0-9]+)?$
RewriteCond $1 !^bugs/
RewriteRule ^(.+)$ bugs/$1 [L]

Or a single rule:

RewriteCond %{HTTP_HOST} ^features\.loc\.?(:[0-9]+)?$
RewriteCond %{REQUEST_URI}>bugs ^/>(.+)$ [OR]
RewriteCond %{REQUEST_URI}>bugs%{REQUEST_URI} ^/[^>]+>(.+)$
RewriteRule !^bugs(/.+)?$ %1 [L]

The ">" character is just an arbitrary character uses as a delimiter to enable parsing. It has no special meaning in regex, and cannot be passed un-encoded is a URL-path, so will rarely occur as part of a real URL-path.

This code uses POSIX regex instead of PCRE, and is therefore compatible with both Apache 1.x and 2.x servers.
Some optimization is very likely possible using PCRE, but I always write code in 'compatibility mode' out of habit.

Jim

beuy

3:04 am on Jul 26, 2010 (gmt 0)

10+ Year Member



Hi JD,

Thanks for the help.

Just to clear things up the host alias is hosted by a separate DNS server within the domain, essentially it just means that a lookup to features.loc or wsrwiki will resolve to the local IP of the machine in question.

I currently am using .htaccess until I can get everything working, after that's done I'll start on moving it away from there to apache configuration files which I'm told is best practice.

The first rule is working with both provided entires but the second of rewriting features.loc/show_bug.cgi?id=19290 to features.loc/bugs/show_bug.cgi?id=19290 dosen't seem to be.

From my testing it looks like the conditions for the provided RewriteCond are not being met, I tried having a crack at fixing it up myself but couldn't get anything workable either.

jdMorgan

3:32 pm on Jul 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you cgi directory is aliased, then the code in .htaccess likely won't be executed, because the Alias will take precedence and divert the request directly to the cgi directory. You'd have to modify the Alias directive to apply only if the URL-path has already been rewritten, and this will only work if the code is all in the config file. Alternately, delete the Alias, and rewrite those script requests directly to the aliased path. You may need to use the [PT] flag on the rule as well.

Sorry, that's a bit disorganized. But check each of those points and let us know. That may help focus my thoughts a bit... :)

Jim

beuy

11:19 pm on Jul 26, 2010 (gmt 0)

10+ Year Member



Hi jd,

Sorry I'm having a bit of trouble checking if the directory is aliased or not, the following is the only line in <IfModule alias_module> tag that is not commented out

ScriptAlias /cgi-bin/ "cgi-bin/"

I think that means that it is in fact aliased?