Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite help

Need to fine tune some simple mod_rewrite

         

briandickens

5:16 pm on Oct 22, 2009 (gmt 0)

10+ Year Member



I was trying to figure out how to do this by looking through tutorials and posts here, but maybe I'm not finding what I need. Anyway, maybe you guys don't mind taking a look at my problem.

My friend has a website that will allow give users a public view of some data. this data would be located at "https://secure.website.com/login/MyLogs.swf?u=username"

What we would like to do is to make it so that if a user goes to:
"http://website.com/mylogs/username" it gets redirected to the above url.

I got this to work with the following:
RewriteRule ^MyLogs/(.+)$ [secure.website.com...] [L]

This works, but it would be ideal if only the friendly url was shown, not the actual. I understand that this happens with external redirection. So, I thought maybe it would be sufficient to have the friendly URL be: "https://secure.website.com/mylogs/username" This shouldn't be too hard. But I thought it would be good to put some rules in place so I currently have this:

RewriteCond %{HTTP_HOST} secure.website.com [NC]
RewriteRule ^MyLogs/([a-zA-Z0-9_-]+)$ /login/MyLogs.swf?u=$1 [NC,L,R]

this sort of works. problems i have are a) i would like to be able to force https and i don't know how to do this. the only example i could find show it in httpd.conf and i dont have access to that.
b) when I have the [R] at the end, it redirects and shows the new URL. When I take it out, i doesn't redirect properly and I don't know what it's doing. I get an invalid username which I am told means I am not providing a username, so it's not passing it properly.

Any ideas?

TheMadScientist

7:19 pm on Oct 22, 2009 (gmt 0)

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



Hi briandickens,

Welcome to WebmasterWorld!

I think this should get you close:

HTTPS
Will contain the text "on" if the connection is using SSL/TLS, or "off" otherwise. (This variable can be safely used regardless of whether or not mod_ssl is loaded).

[httpd.apache.org...]

RewriteCond %{HTTPS} !^on$ [OR]
RewriteCond %{HTTP_HOST} !^(secure\.website\.com)?$
RewriteRule ^MyLogs/([a-z0-9_-]+)$ https://secure.website.com/mylogs/$1 [NC,R=301,L]

RewriteCond %{HTTPS} ^on$
RewriteCond %{HTTP_HOST} ^(secure\.website\.com)?$
RewriteRule ^MyLogs/([a-z0-9_-]+)$ /login/MyLogs.swf?u=$1 [NC,L]

The second A-Z is really unnecessary when using NC... all it does it add processing to the regex. I'm not sure why you're not getting the correct username, because your pattern and back-reference look correct, but I'm not too familiar with https, so I'm not sure if that might have something to do with the issue or not.

jdMorgan

7:24 pm on Oct 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds like you're trying to fix two problems with one piece of code. The following might serve as examples, but may not be exactly what you need. The first rule needs to be put in the "http .htaccess file" if that file is separate from the "https .htaccess file."

The second rule is essentially what you had, but removes the incorrect "R" flag and eliminates "A-Z" from the pattern, since it is redundant if you use an [NC] flag.

You may even need to check %{HTTP_HOST} in that second rule -- it depends on exactly how your server maps domains to directories...


# Redirect to https if secure.example.com requested using http
RewriteCond %{HTTP_HOST} secure\.example\.com [NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://secure.example.com/$1 [R=301,L]
#
# Rewrite 'friendly' URL requests to 'unfriendly' filepath
RewriteCond %{HTTP_HOST} secure\.example\.com [NC]
RewriteRule ^MyLogs/([a-z0-9_\-]+)$ /login/MyLogs.swf?u=$1 [NC,L]

Jim

briandickens

7:47 pm on Oct 22, 2009 (gmt 0)

10+ Year Member



Ok, I used jdMorgan's suggestion and it sort of works. Still getting an error on the second rewrite. Unless I change the flags to [NC,L,R]. Then it works.

Is there a way to log the rewrite without having access to httpd.conf?

briandickens

8:01 pm on Oct 22, 2009 (gmt 0)

10+ Year Member



Okay, i tried using TheMadScientist's code and it worked great. there are errors, but it has to do with having the website -flash app- use different URLs and it can be fixed. So I'm going to go with this.

Thanks a lot! I appreciate the help!

g1smd

5:29 pm on Oct 23, 2009 (gmt 0)

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



Use Live HTTP Headers to examine the HTTP and HTTPS transactions with your server. Be sure that everything is 100% correct for all valid and non-valid URL requests. To not do so risks seeing you back here in a month/three months/a year complaining that Google has delisted your entire site.