Chrome Extension to perform Regex substitution redirect - google-chrome-extension

[{
"id" : 1,
"priority": 1,
"action" : { "type" : "redirect", "redirect": {"regexSubstitution": "\\0#test"} },
"condition" : {
"regexFilter" : "^https://subdomain.domain.com(.*)",
"resourceTypes": ["main_frame"]
}
}]
I am trying to write a short extension to redirect a specific format of web address to a slightly different address; this is for something internal to our organisation, and will make staff members' lives easier.
I have slightly modified my code (above) to remove anything too specific.
The rule returns no errors, but it does nothing. It does not seem to matter how I modify either the filter or the substitution either, I can't get it to do anything - all webpages load normally.
I assume I am overlooking something dumb, but I am not experienced in Chrome Extension development by any means; any help would be appreciated.
With the given code, I would have expected e.g.
'https://subdomain.domain.com/helloworld' to redirect to 'https://subdomain.domain.com/helloworld#test'.
I have struggled to find anything helpful because most examples are too old, and are for lower Manifest versions.

Have you checked that your extension has host permissions under the host_permissions manifest key for the host you're trying to redirect on (https://subdomain.domain.com in your example)? Certain DNR rules require host permissions and without these, the extension will load but the rule will be ignored.
As a note, your rule as written will perform a redirect loop because it redirects to a page that would also match the rule. You'll likely want to find a way to avoid that.

Related

Kentico redirect based off URL

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

301 redirect from legacy site to cakephp site using routes.php and query string

I was looking to build rules to redirect URLs from an old site to a brand new one built with CakePHP 2.5
Originally I was going to write .htaccess rules (though which one of the 3 to use isn't entirely clear) until it occurred to me that I should be able to create 301 redirect rules in routes.php as the Routing page explains.
Unfortunately, though it's rather straightforward to setup rules that redirect www.example.com/page.php to www.example.com/page, it's a little more tricky to setup rules for URLs containing a query string.
For instance, I've been trying to redirect pages that contain pagination that tend to be formed like www.example.com/things/index/page:1 unfortunately, when the original page is formed like www.example.com/things_page.php?page_no=1 I can redirect it to www.example.com/things but I can't carry over the pagination. For this I used:
Router::redirect('/things_page.php?page_no=:page', array('controller' => 'things', 'action' => 'index', ':page' => 'page:[0-9]+'),array('persist' => true, 'status' => 301));
from what I gathered mostly around similar topics on SO. Unfortunately, I can't get my head around getting the framework to interpret that query string as data to be used for the redirected url.
I read somewhere that it wasn't in fact possible in Cake to translate query strings into a clean path that Cake can interpret in the Router::redirect() statement but that strikes me as odd given that I can't be a unique case where I need old links to be translated into a Cake compatible path.
If I could avoid the .htaccess route I would prefer to do so for ease of maintenance but if that's the only way I would need to figure out which of the 3 .htaccess files to edit.
Cheers
Edit:
In the end I found a compromise in a French forum which suggests putting a rule in the beforefilter() function of the AppController like so:
public function beforeFilter(){
if (isset($_GET['id'])) {
$this->redirect(array('controller' => 'things', 'action' => 'view', $_GET['id']));.
}
}
I complemented my code with a string search of $_SERVER['REQUEST_URI'] to isolate the things_page.php for when multiple pages use the same $_GET variable. It's a bit crude but it works
As far as I can tell this is still not supported, I think the follwing ticket is kinda about that issue, not quite sure though:
https://github.com/cakephp/cakephp/issues/2120
Unless there is no support for this, you'll have to go with .htaccess, and the one that you should use to handle the redirects is the one in the webroot folder, ie app/webroot/.htaccess.

IIS URL Rewrite condition file exists using capture groups?

Seems like this would be really easy but can't seem to find an example.
User types in the following URL:
http://www.mysite.com/business/chelan/internet
I want it to go to:
/business/internet_chelan.php
ONLY if the file exists there. Otherwise, go to:
/business/internet.php
"Chelan" is a county in my area. If I want to create a specific page for that county, I could simply add "_chelan" to my file. Otherwise, it just goes to the general internet page.
So I use the following to grab URL parts:
^(business|residential)/([^/]+)/(.+)
and rewrite to:
{R:1}/{R:3}_{R:2}.php
Works beautifully... but I want to have the condition:
if {R:1}/{R:3}_{R:2}.php "Is a file" rewrite to {R:1}/{R:3}_{R:2}.php
But it seems like {R:} does not work in conditions??? Is this true or is there some other way I can write it? I've seen {C:1} and $1 but they don't seem to work either.
Any ideas?
IIS 7.5 on Windows 7 using URL Rewrite module

Is it possible with canonical URL for this pattern in htaccess: /a/*/id/uniqueid?

A big problem is that I am not a programmer….! So I need to solve this with means within my own competence… I would be very happy for help!
I have an issue with a lot of duplicated URLs in the Google index and there are strong signs that it is causing SEO problems.
I don’t have duplicate links on the site itself, but as it once was set-up, for certain pages the system allows all sorts of variations in the URL. As long as is it has a specific article-id, the same content will be presented under an infinite number of URLs.
I guess the duplicates in Google's index has been growing over long time and is due to links gone wrong from other sites that links to mine. The problem is that the system have accepted the variations.
Here are examples of variations that exists in the Google index:
site.com/a/Cow_Cat/id/5272
site.com/a/cow_cat/id/5272
site.com/a/cow…cat/id/5272
site.com/a/cowcat/id/5272
site.com/a/bird/id/5272
The first URL with mixed case is the one used site-wide and for now I have to live with it, it would take too long time to make a change to all lower case. I cannot make a manual effort via htaccess as it is a total of 300.000 articles. I believe there are 10 ‘s of thousands that have one or more duplicates.
My question is this:
Is it possible to create rules for canonical URLs in htaccess in order to make the above URLs to be handled as one as well as for the rest of the 300.000?
I e, is there a way to say that all URLs having
/a/*/id/uniqueid
should be seen as one = based only on the unique ID and not give any regard to the text expressed with the “*”?
My hope is that it would be possible to say that a certain pattern like above should only be differentiated by the last unique segment.
If it is not possible in htaccess, how would it be done with link rel="canonical" on each page, can the code include wildcards?
I should add that the majority of the duplicates are caused by incoming links being lower case where the site itself is using a mix. Would it be OK to assign a canonical URL only with lower case although the site itself is basically always using a mix of lower/upper case?
If this is possible, I would be very happy to be helped with how to do it!!!!
Jonas
Hi Michael! I am not an expert but this is how I think it could be done:
1) My problem is that the URLs have mixed cases and I cannot change that now.
2) If it is OK for the searchengines, it would be fine for me to make the canonical URL identical to the actual URLs with the difference that it was all lower case, that would solve approx 90% of the duplicates. I e this would be the used URL: site.com/a/Cow_Cat/id/5272 and this would be the canonical: site.com/a/cow_cat/id/5272. As I understand, that would be good SEO...or...?
My idea was NOT to change the address browser address bar (i e using 301 redirect) but rather just telling the search engines which URLs that are duplicates, as I understand, that can be done by defining a canonical URL either in htaccess (as a pattern - I hope) or as a tag on each page.
3) IF, it would be possible to find a wildcard solution...I am not sure if this is possible at all, but that would mean it was possible to NOT assign a specific canonical URL but rather a "group pattern", i e "Please search engine, see all URLs with this patter - having the unique identifier in the end - as if they are one and the same URL, you SE, decide which one you prefer": /a/*/id/uniqueid
Would that work? It will only work in htaccess if canonical URLs can be defined as a group where the group is defined as a pattern with a defined part as the unique id.
Is it possible when adding a tag for each page to say that "all URLs containing this unique id should be treated the same"? If that would work it would look something similar to this
link rel="canonical" /a/*/id/5272
I dont know if this syntax with wildcard exist but it would be nice : )
My advice would be to use 301 redirects, with URL rewriting. Ask your webmaster to place this in your apache config or virtual host config:
RewriteMap lc int:tolower
Then inside your .htaccess file you can use the map ${lc:$1} to convert matches to lower case. Here, the $1 part is a match (backreference from brackets in a regex in the RewriteRule) and the ${lc: } part is just how you apply the lc (lowercase) function set up earlier. Here is an example of what you might want in your .htaccess file:
RewriteCond %{REQUEST_URI} [A-Z] #this matches a url with any uppercase characters
RewriteRule (.*) /${lc:$1} [L,R=301] #this makes it lowercase
As for matching the IDs, presuming your examples mean "always end with the ID" you could use a regex like:
^(.+/)(\d+))$
The first match (brackets) gets everything up to and including the forward slash before the ID, and the second part grabs the ID. We can then use it to point to a single, specific URL (like canonical, but with a 301).
If you do just want to use canonical tags, then you'll have to say what you're using code wise, but an example I use (so as not add tags to hundreds of individual pages, for instance) in PHP would be:
if ($_SERVER["REDIRECT_URL"] != "") {
$canonicalUrl = $_SERVER["SERVER_NAME"] . $_SERVER["REDIRECT_URL"];
} else if ($_SERVER["REQUEST_URI"] != "") {
$canonicalUrl = $_SERVER["SERVER_NAME"] . preg_replace('/^([^?]+)\?.*$/', "$1", $_SERVER['REQUEST_URI']);
}
Here, the redirect URL is used if it's available, and if not the request uri is used. This code strips off the query string (this bold bit in http://www.mysite.com/a/blah/12345/?something=true). Of course you can add to this code to specify a custom path, not just taking off the query string, by playing with the regex.

Help with 301 redirects on outgoing links from my site

I work for company that links out to partners through a third party website that tracks them. So for example on our site there will be an outgoing link something like this (names changed to protect my work):
check it out kids!
if you go into link.php, you see I define the link there:
$outlink['chuckecheese'] = "http://partners.linktrackingisprettycool.com/x/212/CD1/$STAMP";
$STAMP is a timestamp and is replaced with, say, "12-25-09-1200" for noon on christmas.
When a user clicks on this link, he goes to www.chuckecheese.com
This all works fine, but it isn't as good for SEO purposes as it could be. I want to make it so that search engines will see it as a link to chuckecheese.com, which which helps our partners' pageranks and is more honest.
I'm in .htaccess trying to make up rewrite rules but I'm confused and don't know exactly how it's done. I tried:
RewriteRule http://www.chuckecheese.com$ link.php?link=chuckecheese$ [QSA]
But this doesn't seem to work. What should I try next?
Thanks in advance for any help. You guys on here are always awesome and I appreciate the part that the good people at stack overflow play in me remaining employed.
You can't use a rewrite rule to redirect the user for this. The request has to be one processed by your webserver.
You might try doing some javascript to achieve this. so the href is to chuckecheese, but onclick, you change the document.location to what you really want to do.
Edited question for bounty
What you could do is pre-process your links based on the user agent of the browser. So when the user-agent is googlebot (one of the below strings), You display the real url of http://www.chuckecheese.com.
Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
Googlebot/2.1 (+http://www.googlebot.com/bot.html)
Googlebot/2.1 (+http://www.google.com/bot.html)
When the URL is not googlebot, you display the link that does traffic analytics.
You can find a list of user agents at the following URLs:
http://www.useragentstring.com/Googlebot2.1_id_71.php
http://www.user-agents.org/
http://www.pgts.com.au/pgtsj/pgtsj0208c.html
If googlebot isn't showing the correct user-agent (or it changes in the future) google recommends you do a reverse look up against the IP address. This will be a small performance hit.
You can verify that a bot accessing your server really is Googlebot by using a reverse DNS look up, verifying that the name is in the googlebot.com domain, and then doing a forward DNS look up using that googlebot name. This is useful if you're concerned that spammers or other troublemakers are accessing your site while claiming to be Googlebot. -Google
Edited for further explanation
Assuming you are using php, you generate the link at runtime. Here is some code I whipped up.
function getRealURL($url)
{
// adjust this regex to match the pattern of your traffic analysis urls
ereg("link=(.+)$",$url,$matches);
if ($matches[1])
{
// adjust this so the urls come out correctly
return "http://www.".$matches[1].".com";
}
else
{
return $url;
}
}
function isGoogle()
{
switch ($_SERVER['HTTP_USER_AGENT'])
{
case 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)':
case 'Googlebot/2.1 (+http://www.googlebot.com/bot.html)':
case 'Googlebot/2.1 (+http://www.google.com/bot.html)':
return true;
default:
return false;
}
}
function showlink($url)
{
$trafficAnalysisUrl = getRealURL($url);
if (isGoogle())
{
return $url;
}
else
{
return $trafficAnalysisUrl;
}
}
<html>
...
Come eat pizza at <a href='<?=showLink("link.php?link=chuckecheese")?>'>chuck e cheese!</a>
...
</html>
I doubt google would care about something like this since both links go to the same place.
But check the TOS to be sure.
http://www.google.com/accounts/TOS
An assumption of yours is not good. You say:
I want to make it so that search
engines will see it as a link to
chuckecheese.com, which helps our
score when people search for chuck e
cheese because we'll be seen as
linking right to them.
If this really helped SEO wise, every body would spam link all great sites just to get SEO pagerank and the game would just be too easy. The beneficiary of a link is the recipient page/site, not the sender.
Hey PG... linking out to other websites will not give you any further PageRank just as having your ads in Adwords appearing on a thousand other sites will not give you PageRank. And yes, your partners are being benefited by having you linked to them. And what about those benefits to be gain that you speak of from being open? From my understanding of what you have wrote, it is just another fancy redirect. Google knows that.

Resources