Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I parse a file and use an if clause where needed?

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
I have a file in this format:

from: to:
from: to:
I want to parse the file to give me Apache RewriteRules and RewriteConds where needed.

I can do the first part without stripping out the " part like this:

/^from:/{from = $2}#;if ($1 ~ /\?) uri=true}
/^to:/{to = $2;print "RewriteRule ^"from" "to " [L,R=301]"}


Which results in output like this:

RewriteRule ^ [L,R=301]

which isn't what I need because I need to strip out the beginning up until the first / on the from: line. It also needs to be updated so that when there's a query string it doesn't use the same format as when there isn't one.

The desired output for a line without a query string is this:

RewriteRule ^/tip/1,page [L,R=301]

And with a query string is this:

RewriteCond %{REQUEST_URI} /path/articles/showarticle\.html [NC]
RewriteCond %{QUERY_STRING} id=mycode
RewriteRule ^(.*)$ [L,R=301]

I know for stripping out the characters I can probably use index and substr, but I'm not too sure about how to handle the if portion.

Any help would be appreciated.

Thanks,

John
 
A starting point:
Code:
/^from:/{from=$2;sub(/http:\/\/[^\/]*/,"",from)}
/^to:/{to=$2;if(from!~/\?/)print "RewriteRule ^"from"     "to " [L,R=301]"}

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV! That helped strip out the domain.subdomain.tld piece!

I need to figure out how to use an if statement properly in order to parse the redirect accordingly.

Thanks,

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top