homepage Welcome to WebmasterWorld Guest from 54.242.18.190
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Please help - I'm dreadful with RegEx
My rules are having side effects
chris_f




msg:1493329
 10:20 am on Jun 23, 2006 (gmt 0)

Hi All,

I am trying to rebuild our companies CMS system. I currently have a number of RegEx rules in place (5 in total) to do what I want, however, they are causing problems elsewhere. Images, css and js docs aren't loading. If I disable the URL Rewritting they work but obviously everything else breaks.

Here are the lines and what I am trying to achive with them.

RewriteRule ^/$ /cms/cms.asp?page=default

Any connections to www.domain.com should be redirected to www.domain.com/cms/cms.asp?page=default

RewriteRule ^/(.*)/$ /cms/cms.asp?page=$1-default

URL's like:
www.domain.com/ordering/
www.domain.com/internet/

Should be redirected to:
www.domain.com/cms/cms.asp?page=ordering-default
www.domain.com/cms/cms.asp?page=internet-default

RewriteRule (.*)\.html$ /cms/cms.asp?page=$1

URL's like:
www.domain.com/contact.html
www.domain.com/ordering/hardware.html

Should be redirected to:
www.domain.com/cms/cms.asp?page=contact
www.domain.com/cms/cms.asp?page=ordering-hardware

RewriteRule .*/(.*)-(.*)\.htm[^lL](.*) /asp/$1/$2.asp?$3

URL's like:
www.domain.com/curriculum/sow-unit.htm?abc=123&def=456

Should be redirected to:
www.domain.com/asp/sow/unit.asp?abc=123&def=456

RewriteRule ([^.]+[^/])$ /cms/cms.asp?page=$1-default

URL's like:
www.domain.com/curriculum

Should be redirected to:
www.domain.com/cms/cms.asp?page=curriculum-default

Ok. Now the whole idea.

URL's to folders should be redirected to the default.html file within that forlder (this includes the root of the website, URLs ending in the slash, URLs not ending in the slash.

HTML files should then be redirected to their relevant CMS page. So:
www.domain.com/folder/folder2/file.html = www.domain.com/cms/cms.asp?page=folder-folder2-file

HTM files should be translated to their relevant ASP application. So:
www.domain.com/folder/app-file.htm?query=strings&remain=intact = www.domain.com/asp/app/file.asp?query=strings&remain=intact

All other files (e.g. images, css, js, docs, xls etc....) should not be rewritten.

PLEASE HELP!
Chris

 

jshanman




msg:1493330
 1:39 pm on Jun 23, 2006 (gmt 0)

I assume this is link re-writing, so it should be looking for "<a href='...", right?

First, get all your href/locations and store the matches in an array:

var myRE = new RegExp("<a[^>]*(?:href)\s*=\s*\'?"?[^\'"\s>]*\'?"?\s?", "ig");
var results = allDocumentText.match(myRE);
var newArry = new Array();

//loop through all results
for(var i =0; i < results.length; i++) {

//try the matches in order
if (regexp1.exec(results[i])) {

//if it's found, run it's replaceString
var newValue = results[i].replace(regexp1,replaceString2);

//replace original href found with new href value
allDocumentText.replace(results[i],newValue);

} else if (regexp2.exec(results[i])) {
newArry[i] = results[i].replace(regexp2,replaceString2);
allDocumentText.replace(results[i],newValue);
} else if (....
.....
}

Here are the regexp that you *may* (UNTESTED!)

First find all .html files under one directory:
var regexp1 = /(https?:\/\/)www.domain.com(?:\/([^\/]+)\/)([^.\/]+)[.]html/i

Replace with:
var replaceString1 = '$1www.domain.com/cms/cms.asp?page=$2-$3';

Next find all .html files under two directories
var regexp2 = /(https?:\/\/)www.domain.com(?:\/([^\/]+)\/)([^.\/]+)\/([^.\/]+)[.]html/i

Replace with:
var replaceString2 = '$1www.domain.com/cms/cms.asp?page=$2-$3-$4

*You can continue adding in "([^.\/]+)\/" for each possible directory level

Next find HTM files:
var regexp3 = /(https?:\/\/)www.domain.com(?:\/[^\/]+\/)([^-]+)-([^.]+)[.]htm(\?[^\'"\s>;]+)?/i

Replace with:
var replaceString3 = '$1www.domain.com/asp/$2/$3.asp$4

Next find domain folders (www.domain.com/ordering/ www.domain.com/ordering)
var regexp4 = /(https?:\/\/)www.domain.com(?:\/([^\/]+)\/?)/i

Replace with:
var replaceString4 = '$1www.domain.com/cms.cms.asp?page=$2-default

Personally, I'd do this with a server side language once on all files.

Hopes this helps, if any of the reg exp do not work, let me know. I didn't test any of them :O

- JS

chris_f




msg:1493331
 9:09 pm on Jun 23, 2006 (gmt 0)

Thanks JS.

This is all for the Ionic URL Rewritting component (it's free so I think I can name it).

I'll try your rules.
Thanks.

chris_f




msg:1493332
 7:12 am on Jun 26, 2006 (gmt 0)

Hi JS,

I have looked over you post again and am confused. I am using server-side URL rewritting via an ASP component. You appear to be doing it via JavaScript. This isn't an option for me as the pages aren't at their actual first locations (just the rewritten locaton).

Regards,
C

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved