Skip to content

Instantly share code, notes, and snippets.

@timneutkens
Last active July 4, 2016 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timneutkens/8f33fde32a8e9c3f42e3e2f193ff5c32 to your computer and use it in GitHub Desktop.
Save timneutkens/8f33fde32a8e9c3f42e3e2f193ff5c32 to your computer and use it in GitHub Desktop.
Fast way to create apache rewrite rules
// Create RewriteRule from given variables
function createRewriteString(oldUrl, newUrl, removeDomainPrefix) {
return 'RewriteRule ^' + oldUrl.replace(removeDomainPrefix, '') + '/?$ ' + newUrl + ' [NC,L,R=301]'
}
// Url to path mapping. Seperated by one space.
const urls = `http://example.com/foo/bar /bar/foo
http://example.com/foo/baz /baz/foo`
// Split by line break
urls.split("\n")
// Map through all paths
.map(function(paths) {
// Split url and path
const urlPaths = paths.split(" ")
// Create rewrite string
const rule = createRewriteString(urlPaths[0], urlPaths[1], 'http://example.com/')
// Log it to the console
console.log(rule)
return rule;
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment