in htaccess redirect url following a rule - facebook

enter code herei have developed a facebook application for example on apps.facebook.com/myappname/, it's a web application hosts on myappname.mydomain.com.
in facebook applications just the url base is different, the second part url is the same, for example:
myappname.mydomain.com/home
apps.facebook.com/myappname/home
myappname.mydomain.com/product
apps.facebook.com/myappname/product
i'd like show just facebook url, not real url so for this reason i need to create a rule redirect in htaccess. i mean a rule which changes just base url from apps.facebook.com/myappname/ to myappname.mydomain.com/
is it possibile? tx, best regards
waiting help i'm working on solution and maybe this is apart of one. in htaccess i added this code
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !apps.facebook.com/myappname$ [NC]
RewriteRule ^(.*)$ https://apps.facebook.com/myappname/$1 [L,R=301]
but as you know facebook application is like an iframe so in this way starts a included loop an my app doesn't work. i need add condition to this rule, "when url is not in facebook iframe" have idea? tx

RewriteCond %{HTTP_HOST} !apps.facebook.com/myappname$ [NC]
This will always be true, because a request reaching your server will never have the HTTP_HOST set to apps.facebook.com. (And the path component of an URL is not part of the domain name …)
The only thing you could do with mod_rewrite in this case is checking the HTTP Referer, I guess – if it’s anything else than apps.facebook.com, then your app is likely being called outside of the FB iframe. (But how unreliable the referer is, should be common knowledge by now.)
Your best bet is to use JavaScript to check if your app is being called within an iframe or on it’s own (and if it is in an iframe, assume it is on apps.facebook.com.)
if(top == window) {
// not in iframe, redirect:
window.location.href = "{your app’s URL on facebook}";
}
This would fire only if your app is called directly in the top browser window – if it is framed, no matter if on facebook.com or not, this code will do nothing.
You could add an X-Frame-Options header in your server config, so that your app will only be allowed to be displayed in an (i)frame on apps.facebook.com, and not anywhere else. If you combine that with the JS approach, you can be pretty sure that if your app is “framed”, it is on apps.facebook.com and not somewhere else.

after two working days i understand this, tx for your reply, this is my javascript solution
<script>
if (parent.location.href == self.location.href) {
window.location.href = 'https://apps.facebook.com/myappname' + window.location.pathname;
//alert(window.location.pathname);
}
</script>
PS. tx to MAX!!!

Related

What is fbclid? the new facebook parameter [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
For two days, I have noticed that the URL that I publish on Facebook, there is a parameter is added:
?fbclid=uFCrBkUgEvKg...
To be more precise something like: http://example.com?fbclid=uFCrBkUgEvKg...
Does anyone know what this parameter does?
What is it for and what is the use of the developers?
Thanks for your comments.
I know that gclid, is short for (Google Click Identifier)
It's a unique tracking parameter that Google uses to transfer information between your Google Ads account and your Google Analytics account.
Facebook must be doing the same thing or something similar with fbclid to improve tracking analytics systems.
This helped me: https://greasyfork.org/en/forum/discussion/44083/fbclid-tracking-parameter-attached-by-facebook
Here is cite from the link:
Put this code in your .htaccess file:
RewriteCond %{QUERY_STRING} "fbclid=" [NC]
RewriteRule (.*) /$1? [R=301,L]
If you work in WordPress:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} "fbclid=" [NC]
RewriteRule (.*) /$1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
As I understand it, the parameter is a means of tracking the site visitor so that if your site includes advertising from Facebook, they can customise it to match the recorded browsing habits of the visitor.
The Apache mod_rewrite solution above is problematic because it strips the entire query string. If the URL already had a query string, this will break it. To strip just the fbclid parameter, it's useful to note that Facebook always appends it to a URL, so it's always last. That simplies the mod_rewrite code a little. This is what I do:
# Strip Facebook spyware tokens
RewriteCond %{REQUEST_METHOD} =GET [NC,OR]
RewriteCond %{REQUEST_METHOD} =HEAD [NC]
RewriteCond %{QUERY_STRING} ^(.*)&?fbclid=[^&]+$ [NC]
RewriteRule ^/?(.*)$ /$1?%1 [NE,L,R=301,E=limitcache:1]
Header always set Cache-Control "max-age=604800" env=limitcache
The E=limitcache:1 flag and Header directive is to limit how long the 301 redirect is cached. By default many browsers cache it literally forever. This reduces that to one week (or 604,800 seconds). I may be in a minority in thinking this, but that seems good practice to me. I don't know how long fbclid tokens persist, but if they're long-lasting, it means Facebook will be directing visitors to the same URLs for a long time, and if you ever want to support Facebook's targeted adverts, or if they start using the fbclid for other functionality that you need, you may find these permanently-cached redirects come back to bite. But if you're willing to risk it, you can delete both the Header directive and the E=limitcache:1 flag.
The two tests of %{REQUEST_METHOD} are to prevent Apache from redirecting POST requests (or more esoteric requests like PUT or DELETE, if they're relevant). Most browsers change the request to be a GET requests on a 301 or 302 redirect, which is explicitly allowed by RFC 7231. There is a new 308 redirect code must not have its method rewritten, but unfortunately it's not supported by Internet Explorer on Windows 7 (and probably never will be).
Another approach, how to remove this parameter (so your users can share your URL without removing it manually) is to use JavaScript and history.replaceState.
All credits go to original author - https://www.michalspacek.cz/zmena-url-a-skryvani-fbclid-pomoci-javascriptu
Code from link:
(function() {
var param = 'fbclid';
if (location.search.indexOf(param + '=') !== -1) {
var replace = '';
try {
var url = new URL(location);
url.searchParams.delete(param);
replace = url.href;
} catch (ex) {
var regExp = new RegExp('[?&]' + param + '=.*$');
replace = location.search.replace(regExp, '');
replace = location.pathname + replace + location.hash;
}
history.replaceState(null, '', replace);
}
})();

How to route a path that ends with index.php in Slim framework

I'm using Slim Framework 2.4.2 and my .htaccess looks like this
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
I've been having some trouble with routing a path that ends with index.php. For legacy support reasons, I need to be able to route /soap/index.php to my custom code, but it keeps going to my notFound handler.
I have a route like the following in my /index.php
$app->get('/soap/index.php', function () {
echo "HERE";
});
However, when I go to /soap/index.php in my browser, it get Slim's 404 Not Found handler. I have tried changing the route to /soap/index2.php, which allows me to see "HERE" in the browser, but when I use /soap2/index.php I get the same results as for original. The /soap/ directory does still exist, but /soap2/ does not, so it doesn't seem that it is Apache causing the issue because both return the same results, the Slim 404 handler. It just seems like Slim gives a 404 for any route that goes to an index.php.
I've also tried creating just a /soap/ route to see if Slim is trying to use that when I access /soap/index.php in the browser, but that still gives me a 404.
For now, I will try to just have the 404 handler detect the issue and perform the action.
In the notFound handler, I am getting "/x.php" for the response from $app->request->getPathInfo(). As I add more letters to the front they end up in the string. It's clipping the first 9 characters, which is how long the filename is. After some more looking, this was a known bug.
It is reported on GitHub here.
Updating to the newest Slim framework fixed the issue.

Helicon ISAPI_Rewrite 301 redirect certain files from one folder to another

We moved our sites one folder to another folder. Some services we had to keep on old location still. So we had to keep old the folder.
We had this on our helicon ISAPI .htaccess file on root of FolderA
RewriteRule ^(\w+)\/(\w+)\/(\w+)\/t_(\d+)\/ /folderA/top.aspx?id=$4&linkki=$0
How do we make 301 redirect to new location (folderB)? I know we could make this.
RewriteRule ^(\w+)\/(\w+)\/(\w+)\/t_(\d+)\/ /folderB/top.aspx?id=$4&linkki=$0
But it is not the same as doing 301 redirect to user (and for search engines).
To redirect the folderA to the folderB, you want to redirect as in your comment in the other answer.
This will redirect /folderA/blabla/blalba/bla/t_2345 to /folderB/blabla/blalba/bla/t_2345
RewriteRule ^/folderA\/(\w+)\/(\w+)/(\w+)\/t_(\d+)$ /folderB/$1/$2/$3/t_$4 [NC,R=301,L]
If the number of folders changes, but they all end in t_digits, you could look for anything between the folderA and the t_digits. e.g., this will redirect /folderA/abcdef/t_1234 to /folderB/abcdef/t_1234
RewriteRule ^/folderA\/(.+)\/t_(\d+)$ /folderB/$1/t_$2 [NC,R=301,L]
You may have to adjust whether to keep the leading slash, depending on how things are configured. Also, your question has a trailing slash, but the comment examples don't, so add or remove a trailing slash depending what you really need.
EDIT:
A side note about the permanent redirect. While debugging this, use [NC,R,L] without the 301. When the redirect is permanent (301), the browser often caches a previous rule. When done testing, change it to permanent. See number 2 and 3 in this answer: https://stackoverflow.com/a/9204355/292060
To make it valid 301 redirect just add the following flag at the end of the rule:
RewriteRule ^(\w+)/(\w+)/(\w+)/t_(\d+)/?$ /folderB/top.aspx?id=$4&linkki=$0 [NC,R=301,L]

301 Redirect Subdirectory Path to Single Directory

As I am in the process of a site migration, I have been struggling with a 301 redirect issue, and would love some help.
I am going to move an entire domain (www.oldsite.com) to a new domain (www.newsite.com). I planned to accomplish this by changing the DNSto point to the new server. On the new site, I am trying to set up 301 redirects in my .htaccess file to provide a destination for any URLs that will not exist.
Most of the redirects are fine:
Redirect 301 /old-dir-1/ /new-dir-1/
... works fine.
However, if I want to also redirect a subdirectory:
Redirect 301 /old-dir-1/subdir/ /new-dir-1/
... the actual result is:
www.newsite.com/new-dir-1/subdir
I do not want to pass through the subdirectory.
I have tried a ton of combinations and tutorials and Rewrite Rules like:
RewriteRule ^old-dir-1/(.*) /new-dir-1/$1 [R=301,L]
And countless other variations... any ideas?
If anyone comes across an issue like this, here is how I solved:
1) I made sure no application logic was conflicting with the redirects. In this particular case WordPress was also using a redirect plugin which needed to be disabled.
2) I revised the order of the lines in my 301 entries, placing the most specific URLs at the top, and the less specific below.
IE, first:
Redirect 301 /old-dir-1/subdir/ /new-dir-1/
and then...
/old-dir-1/ /new-dir-1/

IIS6 - How do I redirect users to another site for every page with a few exceptions?

I have a website setup in IIS 6, let's say it's called http://www.this.com.
I have setup a redirection for this website to http://www.that.com which maintains the directory structure and query parameters as follows:
http://www.that.com$S$Q - using the option "The exact URL entered above"
This works great, whenever someone requests, for example:
http://www.this.com/subfolder/page.aspx?Id=1
then they end up at:
http://www.that.com/subfolder/page.aspx?Id=1
Now, I have one page, actually a handler, http://www.this.com/image.axd, which I do not want to redirect.
What is the syntax for that? I've read the Redirection Using Wildcards section here, but I can't work out how to do what seems to be something straight forward.
Note that image.axd is a handler so I can't just "right click" on it and set the redirection properties as it doesn't physically exist.
I also have a couple of other pages in subfolders which I do not want to redirect, for example:
http://www.this.com/subfolder/donotredirectthispage.aspx
Any help would be appreciated.
Edit: A couple of people have mentioned using ISAPI_Rewrite, for which I'm grateful, but I really don't want to introduce another complexity into the website configuration. IIS seems to imply I can acheive what I want using the ! and 0 through 9 variables.
Is it really not possible to do this using IIS?
My current workaround is to set the redirection properties on ALL folders and pages that I want to redirect except those I do not, but this is a management nightmare.
You could implement a custom error page for the page not found error (404) that does the redirection for you. You'd turn off the redirection in IIS. Build the logic for the redirection in your custom error page. Then configure your web site so that 404 errors redirect to your error page.
If you can install software on your IIS server, I'd recommend using a tool to rewrite your request URLs.
For IIS 6.0 I've used ISAPI_Rewrite and it works really well. It's lightweight and very configurable. There's a "Lite" version available for free and will support your requirements.
You configure the program using a text file containing rules that match HTTP requests and then write actions to perform once a rule is matched. Your instance would probably require a general redirect rule (similar to the one in IIS) and rules for your exceptions.
You should look into the possibility of using a header rewrite module, for example ISAPI_rewrite. There is a free "lite" version available that is enough for your needs.
What this can do for you is the following: Before actual pages are executed on the server, the Request headers are rewritten (or HTTP 301/302 redirects are issued) based on a configurable set of rules. The underlying server sees the remaining requests as if the client really made them in that fashion.
The following rules would leave image.axd requests alone, while redirecting everything else.
# image.axd stays unchanged ("L" is the "last rule" flag)
RewriteCond Host: www.\this\.com
RewriteRule ^.*?\bimage\.axd\b.* $0 [L]
# all requests that have not been stopped by an earlier rule
# end up here ("RP" is the "permanent redirect" flag)
RewriteCond Host: www.\this\.com
RewriteRule .* http://www.that.com$0 [RP,L]