URL Problem in Ajax in PHP - php

When I open my site without "www", like http://mysite.com/, then there is a problem with my website hit counter on the home page, which is done through AJAX.
The problem is that counter Image is not getting displayed.
It is showing blank.
There are similar problem on other pages where I have used AJAX to retrieve data.

To the cross-domain security policy, "mysite.com" and "www.mysite.com" are different domains, therefore AJAX requests aren't allowed between them.
The simplest solution is to take the domain out of your AJAX call and use a relative url, for example "/dir/ajax-callback.php" instead of "http://www.mysite.com/dir/ajax-callback.php"

You can create .htaccess file in your root dir and put this text inside
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
This will make sure that every time user enters http://mysite.com, it gets redirected to http://www.mysite.com
The server has to support .htaccess and mod_rewrite

Related

Prohibit direct access to images URL

I have a blog with images. I do not want that the images are directly accessible through the URL (and also not for Googlebot and other bots)... for example... mysite.com/assets/images/img1... etc. So I thought to password protect the images directory with .htaccess. That worked, only front-end all my images became links, and I had to provide my credentials to make them show. How can I make my images show yet NOT make them directly accessible when typing the corresponding URL and the images URLs (or better yet the images directory) NOT accesible for bots to crawl/index?
Don't go with password protection. The right way to do it would be to filter the requests based on the referer URL. If the request originates from your own site then it's ok. Otherwise the request is trying to get an image directly.
I've found this site with detailed instructions on how to do that: http://altlab.com/htaccess_tutorial.html
Taken from the mentioned site:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://url_to_default_image.gif [L]
Note that you would have to enable mod_rewrite in your Apache server.
Btw, just asking. Why don't just let people get the image directly if they want to?

Using 404 page as redirect system?

I have to move a entire site to a new domain, the old domain will be used for another site, so I wish delete all files in old domain.
Can I delete all file and use 404 error page to redirect traffic to new pages?
example:
if($page == "http://www.site1.com/pagexxx.htm") {
header( 'Location: http://www.site2.com/new_page.html' ) ;
}
It technically and SEO correct?
Another method is to create another php file on your old domain, and in .htaccess add the following:
ErrorDocument 404 /dir/.../file.php
You just need to get the request uri in php and process the url and use the header function to redirect.
If you are doing 1:1 redirection, then just setup a redirect rule on your web server.
If, instead, you want to inform users that the site as moved, and redirect everything to the front page of the new site, I'd do the following:
1.) Create a redirect/rewrite rule on the web server to direct everything to your "This site has moved" page. I'd prefer a rewrite, so that they can easily change existing bookmarks.
2.) Put a message informing them of the move, a link to the new site, and a meta-refresh to automatically redirect after 5-10 seconds.
I would try it in .htaccess create or update an .htaccess file and put it in the root of the old domain with this below. If you are just changing domain names and files locations are still the same, this will redirect all traffic from old to new.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
</IfModule>

Configuring apache to send user to another site?

Is it possible to change this script to send users to another site incase the _escaped_fragment_ is present?
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /snapshots/%1? [NC,L]
e.g.
Could the last line be rewritten so it sends the user to
http://foo.bar.com/?url=the-full-url-with-hashbangs
that is, I want to recreate the "pretty url" from the escaped fragment, and pass that entire url to another site.
That site contains static snapshots that should be returned to the users browser.
[edit]
We are releasing a service that enables javascript sites e.g. angularjs, backbone etc to be indexed by googlebot by hooking in to the _escaped_fragment_ request they make.
see http://crawlr.wombit.se/
I want to be able to give correct examples to the users how to set this up in other environments. we are using this in ASP.NET ourselves.
Nope. that's impossible.
Just because hashbangs never being sent to server

Redirecting image to php page

Is it possible to redirect a user to a php page and then redirect to different image, if the user is requesting for the image ?
For example if user requests for the image or if other website requests for the image, it should be redirected to the php page and then redirected to a different image.
Like if other website requests for http://example.com/images/a.gif, the website will get a different image i.e. http://example.com/images/b.gif.
Is it possible? Let me know if I am not clear with my problem.
Thanks.
Looks like a use case for mod rewrite
with something like:
RewriteRule /images/(.*).gif$ images.php?img=$1
Do you mean you want to redirect to a different image, if the request comes from outside your site? Then you need apache's mod_rewrite, and rules something like this:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://your\.site\.com/ [NC]
RewriteRule a.gif b.gif [L,R]
It means: if the referer is not a part of your site, rewrite every request to a.gif to b.gif.
The RewriteRule specified above would be best, if you're using Apache. You could do it either via the server's config files or through a .htaccess rule if your server supports it. For more info, look to Apache's modrewrite page (for version 2.2): http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
If you're using IIS or another web server, they all allow similar redirects, they just have different ways of implementing them.

How to log every single access to my website with 2 different domains

I have two different domains that both point to my homepage in the same server.
I want to log every single access made to my homepage and log which domain the user used to access my homepage, how can I do this?
I tried mod_rewrite in Apache and logging to a MySQL database with PHP but all I could do was infinite loops.
Any ideas?
EDIT:
By your answers, I see you didn't get what I want...
As far as I know Google Analytics does not allow me to differentiate the domain being used if they both point to the same site and it also does not allow me to see that some files like images were accessed directly instead of through my webpages.
I can't also just use $_SERVER['HTTP_HOST'] cause like I just said, I want to log EVERYTHING, like images and all other files, every single request, even if it doesn't exist.
As for Webalizer, I never saw it differentiate between domains, it always assumes the default domain configure in the account and use that as root, it doesn't even display it. I'll have to check it again, but I'm not sure it will do what I want...
INFINITE LOOP:
The approach I tried involved rewriting the urls in Apche with a simple Rewrite rule pointing to a PHP script, the PHP script would log the entry into a MySQL database and the send the user back to the file with the header() function. Something like this:
.htaccess:
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.net [NC]
RewriteRule ^(.*)$ http://www.domain1.net/logscript?a=$1 [NC,L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.net [NC]
RewriteRule ^(.*)$ http://www.domain2.net/logscript?a=$1 [NC,L]
PHP Script:
$url = $_GET['a'];
$domain = $_SERVER['HTTP_HOST'];
// Code to log the entry into the MySQL database
header("Location: http://$domain/$url");
exit();
So, I access some file, point that file to the PHP script and the script will log and redirect to that file... However, when PHP redirects to that file, the htaccess rules will pick it up and redirect again too the PHP script, creating an infinite loop.
The best thing do would be to parse the server logs. Those will show the domain and request. Even most shared hosting accounts provide access to the logs.
If you're going to go the rewrite route, you could use RewriteCond to check the HTTP_REFERER value to see if the referer was a local link or not.
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.net [NC]
RewriteCond %{HTTP_REFERER} !^(.*)domain1(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domain1.net/logscript?a=$1 [NC,L]
RewriteCond %{HTTP_HOST} ^(.*)domain2\.net [NC]
RewriteCond %{HTTP_REFERER} !^(.*)domain2(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domain2.net/logscript?a=$1 [NC,L]
You may also want to post in the mod_rewrite forum. They have a whole section about handling domains.
If Google Analytics is not your thing,
$_SERVER['HTTP_HOST']
holds the domain that is used, you can log that (along with time, browser, filepath etc). No need for mod_rewrite I think. Check print_r($_SERVER) to see other things that might be interesting to log.
Make sure to still escape (mysql_real_escape_string()) all the log values, it's trivially easy to inject SQL via the browser's user-agent string for example.
So, I access some file, point that file to the PHP script and the script will log and redirect to that file... However, when PHP redirects to that file, the htaccess rules will pick it up and redirect again too the PHP script, creating an infinite loop.
Can you check for HTTP headers in the RewriteCond? If so, try setting an extra header alongside the redirect in PHP (by convention custom HTTP headers start with 'X-' so it could be header('X-stayhere: 1');), and if the X-stayhere header is present, the RewriteCond fails and it doesn't forward the browser to the PHP script.
If, however, you can cron a script to download the server logs and run them through some freeware logfile analyzer, I'd go with that instead. Having two redirects for every request is a fair bit of overhead.. (and if I was more awake I might be able to come up with different solutions)
Does Google Analytics not provide this option? Or could you not parse your server log files?
Why not use the access log facility build in apache?
Apache have a "piped log" function that allow you redirect the access log to any program.
CustomLog "|/path/to/your/logger" common

Categories