Kentico redirect based off URL - kentico

We have sales rep profile pages that need to be access by firstname.lastname. So an example URL would be www.mysite.com/mark.handy. This redirect to Find-A-Rep-Detail.aspx?SalesRepWebID=mark.handy.
We're currently are buildingout at a big complicated URL Rewrite that looks at the extension and goes from there. It works, but means we need to managing this either in IIS or the web.config across multiple servers.
I'm looking at a way to do this within kentico. I know the community site has profile pages that seem to take a user's firstname.aspx and redirect to a profile page.
The kentico solution would have to check the URL and if the extension doesn't match a list (aspx, html, js, css, asmx, etc) then redirect.
Here's the kicker. Our user data isn't in kentico. we have a .asps page that has a user control that grabs the name and returns the html.
I'm looking at the wildcard URL docs: https://docs.kentico.com/k9/configuring-kentico/configuring-page-urls/wildcard-urls
Is this possible in Kentico, or should we stick with the iis/web.config options?

As the Kentico wildcard URL docs state:
If you need to allow dots in user names and use wildcard URLs with
user names at the same time, move the wildcard in the Page URL to the
middle of the URL, for example: /Members/{UserName}/Profile
If this is not an option, and the "firstname.lastname" pattern must be the last part of the URL, a possible approach would be to use the RequestEvents.Begin global event to detect the "firstname.lastname" pattern, and redirect to the appropriate page.
Assuming you are using Kentico 10:
[assembly: RegisterModule(typeof(SalesRepRedirector))]
public class SalesRepRedirector : Module
{
private static readonly Regex SalesRepRegex = new Regex(#"^\/([a-zA-Z]+\.[a-zA-Z]+)$");
public SalesRepRedirector() : base("SalesRepRedirector") { }
protected override void OnInit()
{
RequestEvents.Begin.Execute += Begin_Execute;
}
private static void Begin_Execute(object sender, EventArgs e)
{
var relativePath = RequestContext.CurrentRelativePath;
var regexMatch = SalesRepRegex.Match(relativePath);
if (regexMatch.Success)
URLHelper.Redirect("~/Find-A-Rep-Detail.aspx?SalesRepWebID=" + regexMatch.Groups[1].Value);
}
}
(Place the file in the App_Code folder, or in a separate assembly)
Some things to note:
You need to tweak the regex, to ensure that the redirection does not affect request for files and pages (.aspx, .css, .js...)
There is some overhead involved when using this approach, as every request will be checked for the pattern

I think you can accomplish this within Kentico. Note, I'm simply providing a proof of concept here and have not yet tested or created anything yet.
Add an alias to the home page (assuming this is the root of the website) so it accepts the invalid url with the username. So something like /{UsersID}. Then using the javascript webpart, you can check for the {?UsersID?} url parameter and redirect them to /Find-A-Rep page. You can even go one step further and on the Find-A-Rep page, add the same alias /Find-A-Rep/{UsersID} and then you can use a friendlier url like /Find-A-Rep/Mark.Handy and still use that querystring parameter if you need in code.

I did a quick test, it sort works but not 100% they way you want, because you cannot use "." in the wildcard URL.
You can create the template Find-A-Rep-Detail.aspx, within the template, use {? firstname ?} and {? lastname ?} to get the value.
Then in the URLs property, set a path or pattern to /{firstname}-{lastname }. If you use . instead of -, it will be replaced with -
So the wildcard URL /john-smith will be displayed by get Find-A-Rep-Detail.aspx template directly and you can get the first and last name.
One important thing for wildcard URL is the right pattern, if there is no prefix and the pattern is just /{somehting}, then your regular pages like /aboutus would be in trouble.

Hello I know this might be more of a manual approach in listing all the potential URLs, but have you considered using this URL Redirection module from Kentico - http://devnet.kentico.com/articles/url-redirection?
As you can list all your 'from' and 'to' urls all within Kentico

Related

Remove question mark from URL query

Is there any way I can remove the question mark from URL using Mod_Rewrite like this?:
domain.com/controller-name?parameter/parameter/parameter
to change question mark after controller-name to slash
domain.com/controller-name/parameter/parameter/parameter
I want to add this to htaccess in PrestaShop, where I send query like this: parameter/parameter/parameter to controller.
Normal Presta URL is domain.com/index.php?controller=controller-name and I set on SEO settings friendly URL to this controller to rewrite to only controller-name so SEO friendly URL is domain.com/controller-name .
To send some parameters to this controller I can use URL like this: domain.com/controller-name?(here parameters)
In my controller I grab parameters after question mark ? and show content according to this parameters. The parameters are separated by a slash. So I want to get rid of question mark and replace it by slash to get: domain.com/controller-name/(here parameters) with parameters I want to look like this domain.com/controller-name/parameter/parameter/
When I use URL domain.com/controller-name/parameter/parameter/ shows error 404 and Presta don't see that I want content from controller-name.
I think it is achievable by mod_rewrite rule in .htaccess, but I don't know how to write rule according to this problem.
OR maybe there is a way to add something to controller or SEO configuration in Presta to work with / instead ? in query... that would be great solution...
You can use the history.pushState and history.popState to change the URL in the browser which won't cause a page reload. But the problem is that old browsers don't support this.
Alternatively, you can set the window.location.hash property to a value that contains whatever state information you need, then either use the window.onhashchange event, or for older browsers that don't support onhashchange (IE < 8, Firefox < 3.6), periodically check to see if the hash has changed (using setInterval for example) and update the page. You will also need to check the hash value on page load to set up the initial content.
If you're using jQuery there's a hashchange plugin that will use whichever method the browser supports. I'm sure there are plugins for other libraries as well.
One thing to be careful of is colliding with ids on the page, because the browser will scroll to any element with a matching id.

RewriteRule - redirect multi variable URL to multi variable URL

Our old website has a search URL structure like this:
example.com/Country/United States/Region/California/Area/Southern California/City/San Diego/Suburb/South Park/Type/House/Bedrooms/4/Bathrooms/3/
This is currently rewritten to point to the physical page:
/search/index.aspx
The parameters in the URL can be mixed up in different orders, and the URL can include one or more parameters.
We want to 301 redirect these old URLs to a new structure that is ordered in a logical way and more concise:
example.com/united-states/california/southern-california/san-diego/south-park/?type=house&bedrooms=4&bathrooms=3
example.com/united-states/california/?type=house&bedrooms=4&bathrooms=3
Is there a way with URL rewriting to interrogate the old URL, work out what parameters are existing and then write out the new URL structure?
Even if we can limit it to just the Country, Region, Area, City and Suburb, that may be good enough to at least return some results even if it's not perfect.
Also, spaces should be turned into hyphens and all text made lowercase.
I already have the RewriteRule to turn the new URL structure into a URL to point to a physical page. It's just transforming the old URL in to the new URL I need help with. I've googled endlessly and it's just beyond me!
Can anyone help? Thanks.
Since you already have the old search page with rewriting rules set up for it and which is capable of parsing all parameters you need, the easiest and most appropriate solution I see here is to issue a redirect you require from this old search page's code. Just put the code that composes new URL with all parameters needed and redirects from this page - this should be a lot easier than trying to parse all these parameters in .htaccess and combine them into the new format.

Notes 9, rewriting URLs

How do you rewrite a URL in Notes 9 XPages.
Let's say I have:
www.example.com/myapp.nsf/page-name
How do I get rid of that .nsf part:
www.example.com/page-name
I don't want to do lots of manual re-direct because my pages are dynamically formed like wordpress.
I've read this: http://www.ibm.com/developerworks/lotus/library/ls-Web_site_rules/
It does not address the issue.
If you use substitution rules like the following, you can get rid of the db.nsf part and call your XPages directly as example.com/xpage1.xsp:
Rule (substitution): /db.nsf/* -> /db.nsf/*
Rule (substitution): /* -> /db.nsf/*
However, you have to "manually" generate your URLs without the db.nsf part in e.g. menus because the XPages runtime will include the db.nsf part in the URLs if you use for instance the openPage simple action.
To completely control what is going in and out put your Domino behind an Apache HTTP and use mod_rewrite. On Domino 9.0 Windows you can use mod_domino
You can do it with a mix of subsitutions, "URL-pattern" and paritial refresh.
I had the same problem, my customers wants clean URLs for SEO.
My URLs now looks like these:
www.myserver.de/products/financesoftware/anyproduct
First i used one subsitution to cover the folder, database and xpage part of the URL.
My substitution: "/products" -> "/web/techdemo.nsf/product.xsp"
Problem with these is, any update on this site (with in redirect mode) and the user gets back the "dirty" URL.
I solved this with the use of paritial refreshes only.
Last but not least, i uses my own slash pattern at the end of the xpage call (.xsp)
In my case thats the "/financesoftware/anyproduct/" part.
I used facesContext.getExternalContext().getRequestPathInfo() to resolve that URL part.
Currently i used good old RegExp to get the slash separated parameters back out of the url, but i am investigating a REST solution at the moment.
I haven't actually done this, but just saw the option yesterday while looking for something else. In your Xpage, go to All Properties, and look at 'navigationRules' and 'pageBaseUrl'. I think you will find what you are looking for there.

.htaccess Redirect to a Parameter in the URL

I'm trying to allow for users to promote my affiliate product using Clickbank. Clickbank only supports for an affiilate link to link directly to the homepage of a product.
For instance, an example clickbank affiliate link might look like:
http://example.hop.clickbank.net/ which would take you directly to the homepage of the product you're promoting with the final URL you land on looking something like:
http://exampleproduct.com/?hop=MyAffiliateID
However, you can pass parameters in the original hoplink like this:
http://example.hop.clickbank.net/?page=specific-page
If you visit that link, the final URL you land on looks like this:
http://exampleproduct.com/?hop=MyAffiliateID&page=specific-page
What I need to do, is if the "page" parameter is set, to redirect to the "page" parameter (and if the "page" parameter isn't set, to do nothing).
Using the above example, I would need the htaccess file to redirect this URL:
ttp://exampleproduct.com/?hop=MyAffiliateID&page=specific-page
To this URL:
http://exampleproduct.com/specific-page?hop=MyAffiliateID
I know it's typically proper etiquette to include what I have so far in regard to working on this, but to be perfectly honest, I don't even have a clue as to where to start.
I'm an idiot, I just needed some simple PHP to accomplish this, no .htaccess required.
This is what I'm using to get the above accomplished:
$page = $_GET["page"];
$hop = $_GET["hop"];
if (isset($url)) {
header("Location: " . $page . "?hop=" . $hop);
exit;
}

Redirecting a URL based on first 2 letters in its URL alias

So I have a problem in which I'm attempting to redirect a URL based on whether or not it begins with a certain few letters or numbers. I'm needing to redirect a site visitor if the first 2 of the trailing URL begins with "A2" or "a2", remove the first 2 of the URL and then replace the "A2" or "a2" with a specific directory path.
So here's a specific example to clarify. If the user clicks on (or types in their browser's address bar)
http://www.example.com/A2d39g
or
http://www.example.com/a2d39g
they would automatically be redirected to
http://www.example.com/product/d39g
Also worth nothing, they individual typing in this URL will be reading it off a label in the mail, they won't always be clicking on a link. I originally had thought of doing this through jQuery (note some of the solutions below), but I've modified this posting as I think it would be better to approach this through the .htaccess file.
See a demo of the following here.
The first thing we need to do is bind the click event and prevent the default behavior:
$('.redirMe').click(function(e){
e.preventDefault();
});
Then, within this click function we'll perform a series of splits and substring divisions in order to test the code value. Note that I assume the link is no more complicated than what you've given, that it will always be the root host followed by a simple alphanumeric code.
var th = $(this).attr('href'), // get the href
ah = th.split('/'), // split it up
code = ah[ah.length - 1], // grab the last value (the code)
c2 = code.substring(0, 2), // grab the first two code chars
Then we'll pop the code off the end of the split href to get the host and test the first two characters converted toLowerCase to see if they match a2, and if so redirect to product, otherwise visit a 404 page:
redr = (c2.toLowerCase() == "a2") ?
ah.join('/') + '/product/' + code.substring(2) :
ah.join('/') + '/404;
You can then set window.location.href = redr to redirect!
Again, here's sample code of the above.
You can do this kind of redirection with Apache mod_rewrite. Add something like this to your httpd configuration files:
RewriteEngine On
RewriteRule ^/[aA]2(.*)$|^/(.*)[aA]2$ /product/$1
There is no direct way to do this in JavaScript in a cross-browser manner; many browsers do not allow you to get the full URL from window.location in order to prevent malicious JavaScript from tracking users. The best way to do this would be on the server side. If you want to do a redirection from JavaScript, you would probably need to send an AJAX request to a server and hope that the HTTP referer header indicated the page where it was coming from, so it could respond with the desired action.
I found the solution...I reposted the question slightly differently here and it was answered: Redirect a URL based off it's first character with htaccess

Resources