sorting out mod rewrite

ok, .. so ive been bugging ppl in the channel and i have most of it downpat, but im not 100% sure why this is breaking

what im trying to do is redirect $1.our-lan.com to www.our-lan.com/$1.. my config is as follows

RewriteEngine on
        RewriteCond %{HTTP_HOST}   ^(.+)\.our-lan\.com [NC]
        RewriteCond %{HTTP_HOST}   !^$
        RewriteCond %{HTTP_HOST} !^www
        RewriteRule .*         http://www.our-lan.com/$1 [L,R]
        RewriteLog "/home/nf/rewrite.log"
        RewriteLogLevel 3

in my logs im getting something like

[forums.our-lan.com/sid#80cc410][rid#8227cd8/initial] (2) init rewrite engine with requested uri /
[forums.our-lan.com/sid#80cc410][rid#8227cd8/initial] (3) applying pattern '.*' to uri '/'
[forums.our-lan.com/sid#80cc410][rid#8227cd8/initial] (2) rewrite / -> http://www.our-lan.com/
[forums.our-lan.com/sid#80cc410][rid#8227cd8/initial] (2) explicitly forcing redirect with http://www.our-lan.com/
[forums.our-lan.com/sid#80cc410][rid#8227cd8/initial] (1) escaping http://www.our-lan.com/ for redirect
[forums.our-lan.com/sid#80cc410][rid#8227cd8/initial] (1) redirect to http://www.our-lan.com/ [REDIRECT/302]
[www.our-lan.com/sid#80cc410][rid#822fcf8/initial] (2) init rewrite engine with requested uri /
[www.our-lan.com/sid#80cc410][rid#822fcf8/initial] (3) applying pattern '.*' to uri '/'
[www.our-lan.com/sid#80cc410][rid#822fcf8/initial] (1) pass through /
[www.our-lan.com/sid#80cc410][rid#8229ce0/subreq] (2) init rewrite engine with requested uri /index.html
[www.our-lan.com/sid#80cc410][rid#8229ce0/subreq] (1) pass through /index.html
[www.our-lan.com/sid#80cc410][rid#8229ce0/subreq] (2) init rewrite engine with requested uri /index.cgi
[www.our-lan.com/sid#80cc410][rid#8229ce0/subreq] (1) pass through /index.cgi
[www.our-lan.com/sid#80cc410][rid#8229ce0/subreq] (2) init rewrite engine with requested uri /index.pl
[www.our-lan.com/sid#80cc410][rid#8229ce0/subreq] (1) pass through /index.pl
[www.our-lan.com/sid#80cc410][rid#8229ce0/subreq] (2) init rewrite engine with requested uri /index.php
[www.our-lan.com/sid#80cc410][rid#8229ce0/subreq] (1) pass through /index.php

3 Replies

@Internat:

ok, .. so ive been bugging ppl in the channel and i have most of it downpat, but im not 100% sure why this is breaking

what im trying to do is redirect $1.our-lan.com to www.our-lan.com/$1.. my config is as follows

RewriteEngine on
        RewriteCond %{HTTP_HOST}   ^(.+)\.our-lan\.com [NC]
        RewriteCond %{HTTP_HOST}   !^$
        RewriteCond %{HTTP_HOST} !^www
        RewriteRule .*         http://www.our-lan.com/$1 [L,R]
        RewriteLog "/home/nf/rewrite.log"
        RewriteLogLevel 3


You either need to put parens around the matching in the rewriterule:

RewriteRule (.*)         http://www.our-lan.com/$1 [L,R]

Or change the backreference to refer to the RewriteCond as per the documentation.

RewriteRule .*         http://www.our-lan.com/%1 [L,R]

yeah i was told that just after i posted, but the problem still exists.. i also tried playing arround with the conidition in the first rewritecond changing it from (.+) to (.*) but neither worked…

both basicly do the same thing and it ends up redirecting blog.our-lan.com to www.our-lan.com// <== note the double slash

Already sent this to internat but in case someone else with this problem is scanning the forums looking for the solution here it is.

The problem with what you're doing is that RewriteRule works off of the Request-URI not the entire URL. (i.e. if you type in http://subhost.host.com/path/to/script.php then RewriteRule checks against "/path/to/script.php"). The answer to this is tricksie and but very cute.

(using the particulars of Internats example above)

RewriteEngine on
RewriteCond %{HTTP_HOST}   ^(.+)\.our-lan\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{HTTP_HOST}   !^www
RewriteRule ^(.+)                       %{HTTP_HOST}$1                [C]
RewriteRule ^([^.]+)\.our-lan.com(.*)   http://www.our-lan.com/$1$2   [L,R]

The first RewriteRule injects the Hostname into the URI and then the second one strips it out but saves the parts that we're interested in. All very devious but it works.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct