logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: Redirect is ignoring rewriteRule
Author
dzen



Joined: 15 Jun 2015
Posts: 8
Location: United State, Chicago

PostPosted: Mon 15 Jun '15 22:35    Post subject: Redirect is ignoring rewriteRule Reply with quote

So I am running Apache 2.4 on Windows Server 2008. My Virtualhost looks kinda like this:

Code:
<VirtualHost *.80>
    ServerName www.site1.org
    ServerAlias www2.site1.org
    ServerAlias server1.org
    ServerAlias *.site2.com
    ServerAlias *.site2.org
    ..... 15 more aliases
    Include z:/place/redirects.site
   
    RewriteEngine On

    # redirect 1 group
    RewriteCond %{HTTP_HOST} ^(www\.)?site2\.com\/Thing\/ [OR]
    RewriteCond %{HTTP_HOST} ^(www\.)?site2\.com\/thing\/
    RewriteRule .* http://www.site1.org/stuff/Thingstuff.php [R=302,L]

    # redirect 2 group
    RewriteCond %{HTTP_HOST} ^(www\.)?site2\.com [OR]
    RewriteCond %{HTTP_HOST} ^.*\.site2\.com
    RewriteRule .* http://www.site1.org/fun [R=302,L]

    ----- six more redirect groups
</VirtualHost>


So here is the problem. When I have both these redirect groups in the code, redirect 2 group only works and www.site2.com/Thing goes to www.site1/fun. When I comment out redirect 2 group. The redirect 1 group works fine, but redirect 2 group only goes to http://www.site1.org. I can't figure out what is going on here. Why can't I get both these to work at the same time?
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7373
Location: Germany, Next to Hamburg

PostPosted: Wed 17 Jun '15 17:24    Post subject: Reply with quote

Did you copy the rewrite rules correctly?

Cause the first and second conditions are the same in redirect 1 group.

Why don't you ask in the first condition for the host and in the second condition for the requested url?
Back to top
dzen



Joined: 15 Jun 2015
Posts: 8
Location: United State, Chicago

PostPosted: Wed 17 Jun '15 22:26    Post subject: mistake Reply with quote

James Blond
Thanks for the response.

Answer to question one: I copied it correctly. I am pretty new to the apache and I thought that it needed to be case sensitive.

Answer to question two: I rewrote the conditions this way
Code:

    # redirect 1 group
    RewriteCond %{HTTP_HOST} ^(www\.)?site2\.com\/Thing\/
    RewriteRule .* http://www.site1.org/stuff/Thingstuff.php [R=302,L]

    # redirect 2 group
    RewriteCond %{REQUEST_URI} ^(www\.)?site2\.com [OR]
    RewriteCond %{REQUEST_URI} ^.*\.site2\.com
    RewriteRule .* http://www.site1.org/fun [R=302,L]


And the response was that group 1 redirect functioned correctly, but group 2 redirected to www.site1.org rather than www.site1.org/fun.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7373
Location: Germany, Next to Hamburg

PostPosted: Thu 18 Jun '15 10:42    Post subject: Reply with quote

Whoos I did not see the "Thing and thing". Yes it is case sensitive.
Back to top
dzen



Joined: 15 Jun 2015
Posts: 8
Location: United State, Chicago

PostPosted: Thu 18 Jun '15 16:30    Post subject: No worries, be happy Reply with quote

James, no worries. Thanks for just looking at this.

I have tried this, with no different outcome.

Code:

    # redirect 1 group
    Redirect "http://www.site2.com/Thing" "http://www.site1.org/stuff/Thingstuff.php"

    # redirect 2 group
    Redirect "http://www.site2.com" "http://www.site1.org/fun"


The result doing it this way was that redirect 1 group worked as expected, but the redirect 2 group went to www.site1.org not www.site1.org/fun. If I comment out redirect 1 group, redirect 2 group works as expected.
Back to top
glsmith
Moderator


Joined: 16 Oct 2007
Posts: 2268
Location: Sun Diego, USA

PostPosted: Thu 18 Jun '15 21:02    Post subject: Reply with quote

You were close the first time but you didn't pick up on james's request_uri comment.

the http_host is either site2.com or www.site2.com, so tacking on the /Thing would never match. On your second rewrite, www.site2.com is not part of the request_uri, it's the http_host. /Thing is the request_uri.

These both work for me, from site2.com's vhost config (or an htaccess file in site2.com's documentroot. The Redirects are typically cheaper than rewriting.

Code:
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^(www\.)?site2\.com

    # redirect 1 group
    RewriteCond %{REQUEST_URI} ^/Thing
    RewriteRule .* http://www.site1.org/stuff/Thingstuff.php [R=302,L]

    # redirect 2 group
    RewriteRule .* http://www.site1.org/fun [R=302,L]
or
Code:
    # redirect 1 group
    Redirect "/Thing" "http://www.site1.org/stuff/Thingstuff.php"

    # redirect 2 group
    Redirect "/" "http://www.site1.org/fun"
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7373
Location: Germany, Next to Hamburg

PostPosted: Mon 22 Jun '15 17:49    Post subject: Reply with quote

IIRC 2.4 offers the the if statements.

Code:

<If "$req{Host} != 'www.example.com'">
    RedirectMatch (.*) http://www.example.com$1
</If>


or more oldschool

Code:

<If "%{HTTP_HOST} == 'example.com'">
Redirect permanent / http://www.example.com
</If>
Back to top
dzen



Joined: 15 Jun 2015
Posts: 8
Location: United State, Chicago

PostPosted: Fri 26 Jun '15 17:33    Post subject: Reply with quote

glsmith,

I tried doing it your way, but I got errors when I tested the file. I will post the errors latter today.
Back to top
dzen



Joined: 15 Jun 2015
Posts: 8
Location: United State, Chicago

PostPosted: Fri 26 Jun '15 19:02    Post subject: Mr. Blond Reply with quote

Mr. Blond,

Your last suggestion. The old school suggestion just gave me the same results as my other attempts. Thanks for the suggestions everyone. Still working on this.
Back to top
glsmith
Moderator


Joined: 16 Oct 2007
Posts: 2268
Location: Sun Diego, USA

PostPosted: Fri 26 Jun '15 20:30    Post subject: Reply with quote

It's the 26th and I don't see the errors but I did test it and it worked for me based on what you gave. It did however work too well, for instance

www.site2.com/images still landed at www.site1.org/fun, this could probably be worked around.

One thing that might help (since this is 2.4) is in your httpd.conf change

LogLevel Warn
to
LogLevel Warn rewrite:debug

or for redirect
LogLevel Warn alias:debug

or go for broke
LogLevel Warn alias:debug rewrite:debug

The rewrite at debug will show you how it's thinking and making decisions based on what you have configured. Can be helpful sometimes.
Back to top
dzen



Joined: 15 Jun 2015
Posts: 8
Location: United State, Chicago

PostPosted: Fri 26 Jun '15 22:51    Post subject: Mr. Blond Reply with quote

It is the 26th. At least for a couple more hours. I will take the logging advise and change the levels. I will start small. I don't need to flood the log files.

Baby steps Sparks. Baby steps.
Back to top
glsmith
Moderator


Joined: 16 Oct 2007
Posts: 2268
Location: Sun Diego, USA

PostPosted: Sat 27 Jun '15 4:49    Post subject: Reply with quote

Good quote from "Contact."
Back to top


Reply to topic   Topic: Redirect is ignoring rewriteRule View previous topic :: View next topic
Post new topic   Forum Index -> Apache