Tricksy (for me) rewrite of something already re-written...

Discuss practical ways rearrange URLs using mod_rewrite.

Tricksy (for me) rewrite of something already re-written...

Postby dnovo » Fri Aug 08, 2008 9:22 am

Hi, helpful and knowledgeable folks. Bear with me as I try to explain this as clearly as possible. I work at a large University. We have unix web servers. All our accounts are of the form:

Code: Select all
www2.domain.edu/~username


I have created some rewrite rules to send all visitors, regardless of what url they try, to the fancy dns name:

Code: Select all
username.domain.edu/


All well and good. Works beautifully. It successfully redirects www2.../~username to username.domain.edu, and it successfully strips off any leading www. they try to put in there. I first do the redirect, then the RewriteCond for www. followed by the RewriteRule.

Here's the problem. Some of our sites use scripting that lives in a /cgi-bin/ directory at the root of the public html directory. This scripting breaks when the redirect is in force. Essentially, for the scripting to work, the request must be to:

Code: Select all
www2.domain.edu/~uername/cgi-bin/whatever.php


With the redirect in place, it tries to go to:

Code: Select all
username.domain.edu/cgi-bin/whatever.php


And as there is nothing there, it fails.

So, after my initial redirect and my www stripping, I want to then redirect any requests for /cgi-bin/.* back to www2.domain.edu/~username/cgi-bin/.*

I have tried what I think should work, but it doesn't. Actually, I've tried fourteen things I think should work.

Can anyone set me on the right path?

Thanks,

Danny.
dnovo
 
Posts: 3
Joined: Fri Aug 08, 2008 8:53 am
Location: Lawrence, KS

Postby richardk » Fri Aug 08, 2008 12:06 pm

What have you already got?
Do the two URLs go to the same set of files on the server?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dnovo » Fri Aug 08, 2008 12:48 pm

This is what I have that works:

Code: Select all
RewriteEngine on

# get rid of the exposed unix username url
Redirect permanent /~username/index.shtml http://username.ku.edu/

# permanently redirect from www domain to non-www domain
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

The second bit I actually got from this forum, it's much more generic than what I had.

Here is what I just tried most recently that seemed to have no effect whatsoever.

Code: Select all
RewriteCond %{REQUEST_URI} ^/~username/cgi-bin/whatever\.php$ [NC]
RewriteRule ^/~username/cgi-bin/whatever\.php$ http://www2.ku.edu/~username/cgi-bin/whatever.php

I wasn't sure, if I need to redirect the url to a different url, not just a different path, if I maybe need to do a Redirect, instead of a Rewrite... is that possible? Can i to a Redirect after a RewriteCond? Or is Redirect conditional on it's own? And is there a forum for that?

I'm very confused. At this point I've been going around for three days, and I just need someone to tell me what to do, and I'll figure out why it works later. Or not.

Thanks,

Danny.
dnovo
 
Posts: 3
Joined: Fri Aug 08, 2008 8:53 am
Location: Lawrence, KS

Postby richardk » Fri Aug 08, 2008 4:39 pm

Redirect is part of mod_dir, not mod_rewrite. You cannot mix them (a RewriteCond and a Redirect).

Where are you putting the mod_rewrite?
You're only redirceting /~username/index.html, not all of /~username?
Do you want to redirect all of /cgi-bin to /~username/cgi-bin?

Try
Code: Select all
Options +FollowSymLinks

Redirect 301 /~username/index.shtml http://username.ku.edu/
Redirect 301 /cgi-bin/whatever.php  http://www2.ku.edu/~username/cgi-bin/whatever.php

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dnovo » Mon Aug 11, 2008 4:49 am

richard,

You are correct, I do not want to redirect just index.shtml, I do want to redirect everything. But if I just redirect the entire domain, it breaks the cgi-bin, so I leave the index.shtml in there so that the home page redirects, but none of the other pages do. For now.

But yes, I do want to redirect all of /~username, and then, when they request /cgi-bin/anything I want to redirect all of those back to ~username/cgi-bin

Unfortunately, the code you provided fails to re-redirect the cgi-bin request.

When I enter a direct link in the browser to:

http://username.ku.edu/cgi-bin/whatever.php

It throws a 404 error.

I don't know if this matters, but the link on the page that is calling the cgi-bin is pulling the php file in with an ssi include.

Code: Select all
<img src="/images/<!--#include virtual="/cgi-bin/whatever.php" -->" />

Thanks again for all your help,

Danny.
dnovo
 
Posts: 3
Joined: Fri Aug 08, 2008 8:53 am
Location: Lawrence, KS

Postby richardk » Wed Aug 13, 2008 12:21 pm

Where are you putting the mod_rewrite?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www2\.example\.edu$ [NC]
RewriteCond %{REQUEST_URI} !^/~([^/]+)/cgi-bin(/.*)?$ [NC]
RewriteRule ^/~([^/]+)(/(.*))?$ http://$1.example.edu/$3 [R=301,L]

RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.edu$ [NC]
RewriteRule ^/cgi-bin(/(.*))?$ http://www2.example.edu/~%1/$2 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 21 guests

cron