Restrict access to pages to Facebook's OpenGraph scraper - .htaccess

Is there some way to restrict access to one our pages to only allow Facebook's OpenGraph scraper system? We have multiple likes on one page (similar to Digg) and each 'like' needs its own OpenGraph tags which we have on separate pages via the page story.php?1 and ?2 etc. We do not want the user to be able to view story.php as all they contain are the og: tags.
EDIT: It looks to be something I can do using the info in this post: http://facebook.stackoverflow.com/questions/7197919/how-can-i-move-a-url-via-301-redirect-and-retain-the-pages-facebook-likes-and-o
How can I exclude a particular domain from a HTTP 301 redirect. Can you help?

In the end I opted to add this to the story.php PHP code and it works perfectly:
<?php
if ($_SERVER["HTTP_USER_AGENT"] != "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)") {
redirect('http://www.mywebsite.com', 302);
}
function redirect($url, $type=302) {
if ($type == 301) header("HTTP/1.1 301 Moved Permanently");
header("Location: $url");
die();
}
?>

Related

How to redirect Hard Coded Urls to hard coded Urls without hitting errorAction?

I have almost one thousand links which I want to redirect when a request is made specifically to those URLs. e.g
Redirect http://serverAddress/app/2017/06/09/8-20-taraweeh/ http://serverAddress/app/prayer/8-or-20-Rakat-for-Taraweeh
I am trying this code in .htaccess file but the problem is that when I enter the Url, instead of hitting the .htaccess code it first hits the Yii app and check if the Url exists, if not exists(definitely does not exist) it takes me to the error page.
Now I cannot define a rule because as you can see the URL after the app is changing, I cannot define a generic rule. So I have to hard code in my yii2 app.
Question:
How to Hard code Url redirections in yii2?
You could define a rule something similar:
'<year:\d+>/<month:\d+>/<day:\d+>/<title>' => 'site/redirect'
Then in your SiteController use can use redirect in the controller action:
public function redirectAction($year, $month, $day, $title) {
$newUrl = .... // compute new URL based on the parameters
return $this->redirect($newUrl, 301);
}

Using # in landing page url

I'm using mysite.com/#contact as a landing page URL in some of the PPC campaigns, but when Google or Bing ads their URL parameters, I get url like mysite.com/#contact?campaign=x&source=x.. and the browser doesn't scroll down to the #contact anchor.
Any way to fix this? Or do I need to use a different URL?
In order to scroll down to the anchor it has to be at the very end of your url mysite.com?campaighn=x&source=x[...]#contact
I never used PPC ads so I don't know how to fix it, but at least that's the issue.
You could try to detect the URL parameter and use JavaScript to scroll to the specific location if true.
Untested combination of PHP / JS:
<?php if ( isset( $_GET['campaign'] ) { ?>
<script type="text/javascript">
//JS stuff
</script>
<?php } ?>

How to redirect ANY external links to a specific url format using .htaccess?

I want to redirect all the external links, i.e., links like http://www.someothersite.com/anything in my site(say http://www.example.com) to http://www.example.com/something.
Here the problem I am facing is how to take care of http://www.someothersite.com/anything because its value is not fixed. It could be from any different domain.
First of all, .htaccess configures an HTTP Server which handles incoming requests, requests for resources on your server, not external ones. But since you've explicitely asked for a .htaccess way of redirecting external links, I will outline three approaches:
Use .htaccess and mod_substitute to replace all external links on your pages with a fixed internal one.
Use PHP and output buffering to replace all external links with internal ones.
Use JavaScript to redirect external links to internal ones on click.
All methods have advantages and disadvantages.
1. .htaccess and mod_substitute
If have not tested this, but theoretically, it should work.
Place the following in your .htaccess and replace www.yourdomain.com with your domain:
AddOutputFilterByType SUBSTITUTE text/html
Substitute "s|(<a\s[^>]*)href=\"https?://([^/]+)(?<!www\.yourdomain\.com)/[^\"]*\"|$1href=\"http://www.yourdomain.com/your-fixed-link\"|i"
This adds an output filter to all request bodies with HTML content and uses a regular expression to replace any external URL in the href attribute of every link with your fixed one.
Drawbacks:
Your domain must be fixed (sub2.yourdomain.com and yourdomain.com will be considered external).
You must use an Apache HTTP server which has mod_substitute loaded.
2. PHP and output buffering
If you already use PHP to generate your pages, you can add output buffering with a callback which replaces external links with internal ones:
<?php
function replace_links($html)
{
return preg_replace('~(<a\s[^>]*)href="https?://([^/]+)(?<!www\\.yourdomain\\.com)/[^"]*"~i', '$1href="http://www.yourdomain.com/your-fixed-link"', $html);
}
ob_start('replace_links');
// generate page
?>
This will prevent PHP from sending the content of the page directly to the browser, but writes everything to an internal buffer. At the end of the script, the callback is executed and may modify the content of that buffer before it will be sent to the browser.
Advantages over the .htaccess method: More control over the substitution. You could exclude more than one internal domain from the replacement by using preg_replace_callback in combination with a callback which checks the actual domain in the link. Furthermore, the substituted URL does not have to be fixed.
3. Redirecting with JavaScript
The following JavaScript probably is the most popular way of doing what you want:
(function() {
function redirectLink(evt) {
var matches = /^https?:\/\/([^\/]+)\//i.exec(this.href);
if (matches && matches[1] != 'www.yourdomain.com') {
window.location.href = 'http://www.yourdomain.com/your-fixed-link';
evt.preventDefault();
return false;
}
}
var links = document.getElementsByTagName('a'), i;
for (i = 0; i < links.length; i++)
links[i].addEventListener('click', redirectLink, false);
})();
Just make sure that this code is placed after all links (e.g. at the end of the document) or is executed on the DOMContentLoaded event.
Advantages over the .htaccess method: Same as with the PHP method described above.
Disadvantages: The user must have JavaScript enabled, so this is not guaranteed to work always.

ModX redirect based on query string (Revolution 2.3)

I am rebuilding a website in ModX and I want to redirect the old URLs to the new ModX pages, automatically.
An old URL is of the form, http://www.oldsite.com/?pg=2
Every page is like this, so I need to manually map the old page IDs to the new ModX resource IDs. For example, pg=2 is the contact page, which is now resource ID 11, so I'll end up with a map like [2=>11, 3=>15, etc]
If I tweak the main index.php right in the docroot, this does exactly what I want:
/* execute the request handler */
if (!MODX_API_MODE) {
if (isset($_GET["pg"])) {
if ($_GET["pg"] == 2) {
$url = $url = $modx->makeUrl(11);
$modx->sendRedirect($url);
}
else {
// Page is set, but we don't have a redirect for it.
$modx->handleRequest();
}
}
else {
$modx->handleRequest();
}
}
However, I am not happy with hacking index.php directly. I'm a bit short of ModX experience to know exactly where to put this code. I tried:
A snippet, which I then called from my HTML header before any HTML, but the redirect stopped working
The Redirector extra, but this doesn't work on the QUERY_STRING, I don't think
Any insight is appreciated, for the best place to package this code, or a pointer towards an Extra I should be using.
The solution that worked for me, following Sean's insights below, is a plugin. The plugin code is below. For other plugin newbies like me, ensure you visit the "System Events" tab to enable your plugin for the event you're trying to access.
<?php
if ($modx->event->name == 'OnWebPageInit') {
// Look to see if the GET params include a pg. If they do, we have a request
// for one of the old pages.
if (isset($_GET["pg"])) {
// Map the old pg IDs to the new resource IDs.
if ($_GET["pg"] == 2) {
$url = $modx->makeUrl(11);
}
// Add more here...
// When done trying to match, redirect.
// But only do the redirect if we found a URL.
if (isset($url)) {
$modx->sendRedirect($url, array('responseCode' => 'HTTP/1.1 301 Moved Permanently'));
exit;
}
}
}
My preference to do this is in the .htaccess file with redirects or url rewriting - that way you can send the redirect and the response code ~before~ modx has to process anything [save a bit of overhead]
if you still want to do this in modx, take a peek at the sendRedirect docs & send the correct response code [so google gets the hint that the page has actually moved] Note: the $responseCode option is depreciated and you should use it in the options array these days:
$modx->sendRedirect('http://modx.com',array('responseCode' => 'HTTP/1.1 301 Moved Permanently'));
I do agree with not hacking the index.php file, only will cause you grief. What you want to do is place your redirect code in a plugin. Check the Modx API docs for the appropriate event for it to fire on - perhaps: OnWebPageInit will do the trick. Sorry, I don't know exactly which one will work.
HOWEVER ~ IMPORTANT NOTE!
Not all events are actually active, they may show up in the modx manager but don't actually do anything, you will just have to test or dig through the code to find out. [or ask in the community] Again, sorry, I don;t know for sure which ones work and which don't.

.htaccess redirect with a variable?

I need your help.
I want to redirect a URL like:
http://test.mydomain.com/tag/4
to
http://test.mydomain.com/tag/tag.php?id=4
The number is a variable and can change of course. Hope you have a solution for me.
RewriteEngine On
RewriteBase /tag
and now?..
I'm a total newbie in .htaccess!
Thanks for your short help
Simply add this line to your .htaccess file
Redirect 301 http://test.mydomain.com/tag/4 http://test.mydomain.com/tag/tag.php?id=4
This is best done using the pages path. Here's an example:
Redirect 301 /tag/4.html http://test.mydomain.com/tag/tag.php?id=4
This should give you all you need http://www.htaccessredirect.co.uk/
Given that you're talking about redirection from said page using what the user types:
Create a text box and a button like this <button onclick="redit();">
Then add this function
<script type="text/javascript">
function redir(){
var page = document.getElementById("textArea").value;
location.href = "http://test.mydomain.com/tag/tag.php?id=" + page;
};
</script>
This will send the user to the page you want them to, based on what they selected as page number.

Resources