Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite dynamic-to-dynamic URL rewriting

         

Yvoinov

12:21 pm on Jul 3, 2007 (gmt 0)

10+ Year Member



Hi guys,

I've a problem. I use Apache 1.3.x with mod_plsql and need to rewrite some dynamic URL's under https and another back under http.

I've try to write simple test rule:

RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} ^/pls/f(.*)$
RewriteRule (.*) [%{SERVER_NAME}%{REQUEST_URI}...] [L]

Great. It' works perfectly. Now I need to write complete rule for next actions:

1. Single URL http://example.com/pls/f?p=4550:1:(.*) must be rewrited to
[hostname...]
2. Any other URLs http://example.com/pls/f?p=4550:(.*) must remain under http.

Now I've simplify my task and tried to bit extend first working example as:

RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} ^/pls/f\?p(.*)$
RewriteRule (.*) [%{SERVER_NAME}%{REQUEST_URI}...] [L]

Doesn't rewrite.

Kick me, if I stupid - or something else - but help me. How to rewrite one dynamic URL to another dynamic URL?

PS. Yes, I've read all manuals/links/examples I can find, including this site. I found nothing about my task.

[edited by: jdMorgan at 2:14 am (utc) on July 4, 2007]
[edit reason] exmple.com, disable smily-faces in code. [/edit]

jdMorgan

2:23 am on Jul 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The REQUEST_URI can/should be handled in the RewriteRule.

The query string must be handled separately in a RewriteCond.

No special provision is needed to pass the query string through; This is the default behaviour unless it is modified or replaced in the RewriteRule substitution URL.

Try:


RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{QUERY_STRING} ^p=4550:1:
RewriteRule ^/?pls/f$ https://%{SERVER_NAME}/pls/f [L]

Query string handling is described in the Apache mod_rewrite documentation in the RewriteCond and RwriteRule sections.

I wasn't sure if this code is for httpd.conf or for .htaccess, so I coded the RewriteRule pattern to work in either.

Jim

Yvoinov

4:01 am on Jul 4, 2007 (gmt 0)

10+ Year Member



Great, it's works perfectly! Thanks, Jim!
Now will try to write reverse rule for put all other URL's back to http... Will write it here when complete.

Yvoinov

6:18 am on Jul 4, 2007 (gmt 0)

10+ Year Member



Yes. Direct rule to rewrite to https works. But..... reverse rule to put back after login page 1 is not:

# Redirect all non-secure pages to HTTP
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{QUERY_STRING}!^p=4550:1:
RewriteRule ^/?pls/f$ [%{SERVER_NAME}...] [L]
# Resirect 1 secure page to HTTPS
RewriteCond %{SERVER_PORT}!^443$
RewriteCond %{QUERY_STRING} ^p=4550:1:
RewriteRule ^/?pls/f$ [%{SERVER_NAME}...] [L]

The first rule not working... why? :(

It must works as secured login page:

->put onto https->LOGIN->put back to http

The number "1" in query is login page number. It must be under https, all other pages must not. But after first rule triggered, all other pages remains under https...

PS. This rules uses in httpd.conf

Yvoinov

8:30 am on Jul 4, 2007 (gmt 0)

10+ Year Member



Hmmmmm.... When I turn on logging....
Looks like reverse rule (back to http) is completely ignored:

*.*.209.9 - - [04/Jul/2007:14:21:50 +0600] [hostname/sid#5b9808][rid#51063e0/initial] (2) init rewrite engine with requested uri /pls/f
*.*.209.9 - - [04/Jul/2007:14:21:50 +0600] [hostname/sid#5b9808][rid#51063e0/initial] (3) applying pattern '^/?pls/f$' to uri '/pls/f'
*.*.209.9 - - [04/Jul/2007:14:21:50 +0600] [hostname/sid#5b9808][rid#51063e0/initial] (4) RewriteCond: input='80' pattern='^443$' => not-matched
*.*.209.9 - - [04/Jul/2007:14:21:50 +0600] [hostname/sid#5b9808][rid#51063e0/initial] (3) applying pattern '^/?pls/f$' to uri '/pls/f'
*.*.209.9 - - [04/Jul/2007:14:21:50 +0600] [hostname/sid#5b9808][rid#51063e0/initial] (4) RewriteCond: input='80' pattern='!^443$' => matched
*.*.209.9 - - [04/Jul/2007:14:21:50 +0600] [hostname/sid#5b9808][rid#51063e0/initial] (4) RewriteCond: input='p=4550:1:2044720970703470' pattern='^p=4550:1:' => matched
*.*.209.9 - - [04/Jul/2007:14:21:50 +0600] [hostname/sid#5b9808][rid#51063e0/initial] (2) rewrite /pls/f -> [hostname...]
*.*.209.9 - - [04/Jul/2007:14:21:50 +0600] [hostname/sid#5b9808][rid#51063e0/initial] (2) implicitly forcing redirect (rc=302) with [hostname...]
*.*.209.9 - - [04/Jul/2007:14:21:50 +0600] [hostname/sid#5b9808][rid#51063e0/initial] (1) escaping [hostname...] for redirect
*.*.209.9 - - [04/Jul/2007:14:21:50 +0600] [hostname/sid#5b9808][rid#51063e0/initial] (1) redirect to [hostname...] [REDIRECT/302]REDIRECT/302]

I.e, the second rule (put under https) works, but back rule is gone.

Not understanding - why? Error in RuleCond? But where?

AlexK

9:03 am on Jul 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Check the precise location within httpd.conf for your rewrite rules...

SSL (and hence `https' requests) are commonly dealt with via an included-in ssl.conf. If your rewrite-set is below the

"Include conf.d/*.conf"
line, it may never hit those commands.

Yvoinov

9:10 am on Jul 4, 2007 (gmt 0)

10+ Year Member


Yes, Include...ssl.conf directive is above mod_rewrite rule set.

No, http->https redirects all works. As seen in mod_rewrite log above.

Only https->http redirect still completely ignoring.

Once more: the rule set are:

# Redirect non-secure pages to HTTP
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{QUERY_STRING}!^p=4550:1:
RewriteRule ^/?pls/f$ http://%{SERVER_NAME}/pls/f [L]

# Resirect secure pages to HTTPS
RewriteCond %{SERVER_PORT}!^443$
RewriteCond %{QUERY_STRING} ^p=4550:1:
RewriteRule ^/?pls/f$ https://%{SERVER_NAME}/pls/f [L]

# Redirect another Application under HTTPS
RewriteCond %{SERVER_PORT}!^443$
RewriteCond %{QUERY_STRING} ^(p=110:¦p=AppName:LOGIN:[smilestopper])
RewriteRule ^/?pls/f$ https://%{SERVER_NAME}/pls/f [L]

The rule 1 (non-secure pages) is ignored. The rules 2 and 3 works.

The mod_rewrite log contains no information about 1 rule usage.

Why?

Yvoinov

9:14 am on Jul 4, 2007 (gmt 0)

10+ Year Member


In additional.
--------------

Here is a piece of my httpd.conf :

.................
# Include the SSL definitions and Virtual Host container
include "C:\oracle2\product\10.2.0\db\Apache\Apache\conf\ssl.conf"

# Include the mod_osso configuration file
#include "C:\oracle2\product\10.2.0\db\Apache\Apache\conf\mod_osso.conf"

# Include the Oracle configuration file for custom settings
include "C:\oracle2\product\10.2.0\db\Apache\Apache\conf\oracle_apache.conf"

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteLog c:\rewrite.log
RewriteLogLevel 5

# Redirect non-secure pages to HTTP
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{QUERY_STRING}!^p=4550:1:
..........

So, SSL configured, works perfectly, uses in rewrites TO http correctly. But BACK rewrite rule is ignored.

jdMorgan

3:46 pm on Jul 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I agree with AlexK above -- The location of the code is important.

Also, be sure to restart your server, and completely flush your browser cache before testing.

Jim

Yvoinov

4:52 am on Jul 5, 2007 (gmt 0)

10+ Year Member



Ok. But I can't see, where I can wrong in my httpd.conf.

BTW, where must be mod_rewrite directives in httpd.conf in general?

[edited by: Yvoinov at 5:04 am (utc) on July 5, 2007]

Yvoinov

4:59 am on Jul 5, 2007 (gmt 0)

10+ Year Member


Ok.

Here is more complete piece of my Apache config. Where is error? I don't see any.

# Loading rewrite_module here so it loads before mod_oc4j
LoadModule rewrite_module modules/ApacheModuleRewrite.dll

# Include the SSL definitions and Virtual Host container
include "C:\oracle2\product\10.2.0\db\Apache\Apache\conf\ssl.conf"

# Include the mod_osso configuration file
#include "C:\oracle2\product\10.2.0\db\Apache\Apache\conf\mod_osso.conf"

# Include the Oracle configuration file for custom settings
include "C:\oracle2\product\10.2.0\db\Apache\Apache\conf\oracle_apache.conf"

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteLog c:\rewrite.log
RewriteLogLevel 5

# Redirect non-secure pages to HTTP
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{QUERY_STRING}!^p=4550:1:
RewriteRule ^/?pls/f$ http://%{SERVER_NAME}/pls/f [L]

# Resirect secure pages to HTTPS
RewriteCond %{SERVER_PORT}!^443$
RewriteCond %{QUERY_STRING} ^p=4550:1:
RewriteRule ^/?pls/f$ https://%{SERVER_NAME}/pls/f [L]

# Redirect Application under HTTPS
RewriteCond %{SERVER_PORT}!^443$
RewriteCond %{QUERY_STRING} ^(p=110:¦p=AppName:LOGIN:[smilestopper])
RewriteRule ^/?pls/f$ https://%{SERVER_NAME}/pls/f [L]

</IfModule>

Yvoinov

5:03 am on Jul 5, 2007 (gmt 0)

10+ Year Member



Jim, sorry.... I'm not idiot :) .... not complete. Of course, I never ever forget to restart services which read config just once :) Also I always clear browser's cache, because I use SSO cookies and authorizations caching ;)

But - I'm not understand, why http rule still not works? I can't see any obvious mistakes. Did You see? Please, help.

Yvoinov

7:06 am on Jul 5, 2007 (gmt 0)

10+ Year Member


Oh. my god! I'm an idiot.

It means, that reverse rules MUST be in context https virtual host, right?

You right, Jim, thanks Alex.

Finally:

Rewrite rules for http-> https must be in common httpd.conf context.
Reverse rules - https-> http must be in context SSL virtual hosts.
--------------------------------------------------------------------

Thank You for Your help, Jim.

[1][[b]edited by[/b]: Yvoinov at 7:17 am (utc) on July 5, 2007][/1]