i am running a website and my website root directory have .htaccess and index.php, now i want to redirect all user's/traffic to "Under-Construction.php" page except my ip address.
i.e
www.example.com redirect to "www.example.com/under-construction/under-construction.php"
except my ip address.
How can do this with .htaccess.
How can do this with index.php.
example: if $_SERVER['REMOTE_ADDR'] is 123.123.123 redirect index.php else redirect "www.example.com/under-construction/under-construction.php"
Regards.
Like this with php. Put the code at the top of index.php.
if (!in_array(#$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '123.123.123.123')))
{
header('Location: http://www.example.com/under-construction/under-construction.php');
exit;
}
I tested this on my server:
<?php
// echo $_SERVER['REMOTE_ADDR'];
// For proof of concept
if($_SERVER['REMOTE_ADDR'] == "123.123.123.123") {
header( 'Location: http://www.google.com/' ) ;
}
?>
(Obviously that's not my ip ;)
Just as a side note, this may not be the best way to filter incoming clients, especially if you don't have a static ip address that you'll be requesting from. Perhaps set a cookie on your machine that tells the site who you are? Or add a $_GET parameter i.e. if $_GET['dev'] == true then redirect.
Use this .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/index.php$
RewriteCond %{REMOTE_ADDR} !^127.0.0.1$ # replace 127.0.0.1 with your IP address
RewriteRule ^ http://www.example.com/under-construction/under-construction.php
</IfModule>
Note: Replace 127.0.0.1 with your IP address
Related
I wonder if it's possible to redirect 2 different domains vice versa. Suppose I have 2 domains such as www.abc.com and www.xyz.com. Now if any viewer clicks www.abc.com, it should redirect them to www.xyz.com and when any viewer will click www.xyz.com, they will be redirected to www.abc.com. Is there any feature or hosting service that can do this? I know redirection can be simply done using PHP header('Location') but once I did that, it will be redirected to the location site, there's no way to get the original site. Can anyone please help? Thanks.
Assuming that when going www.xyz.com we wants to redirect user to www.abc.com and when going to www.abc.com we wants to redirect user to www.xyz.com
By using the sessions, we can do.
<?php
//on abc.com
session_start();
if(!isset($_SESSION['abc']) || $_SESSION['abc'] != true ){
$_SESSION['xyz'] = true;
header('Location: www.xyz.com');
}
now,
<?php
//on xyz.com
session_start();
if(!isset($_SESSION['xyz']) || $_SESSION['xyz'] != true ){
$_SESSION['abc'] = true;
header('Location: www.abc.com');
}
First I would make a strong case not to do this with your client. But... using .htaccess, we can set a query string flag with the redirect to prevent an infinite redirect loop.
If the address is for www.abc.com we redirect to www.xyz.com only if the flag (signaling that we weren't already redirected) doesn't exist
This is untested, but the concept will work.
RewriteEngine On
#abc => xyz
RewriteCond %{HTTP_HOST} ^www\.abc.com
RewriteCond %{QUERY_STRING} !^flag=fromxyz$
RewriteRule ^(.*)$ www.xyz.com?flag=fromabc [L,R=301]
#xyz => abc
RewriteCond %{HTTP_HOST} ^www\.xyz.com
RewriteCond %{QUERY_STRING} !^flag=fromabc$
RewriteRule ^(.*)$ www.abc.com?flag=fromxyz [L,R=301]
You can also accomplish the same thing with php - by appending a query string to the redirect and testing for it before the header() redirect. Something like:
if ($_SERVER[HTTP_HOST] == "www.abc.com"){
if ($_SERVER[QUERY_STRING] !== "flag=fromxyz"){
header("Location: www.xyz.com?flag=fromabc.com");
die();
}
}
if ($_SERVER[HTTP_HOST] == "www.xyz.com"){
if ($_SERVER[QUERY_STRING] !== "flag=fromabc"){
header("Location: www.abc.com?flag=fromxyz.com");
die();
}
}
The .htaccess method will be more efficient as the server won't have to load a php file before redirecting.
Also, the use of $_SESSION variables is an option as mentioned by #AnimeshSahu. However, they may not be supported in all cases (e.g. Googlebot)
You can use the referer
RewriteCond %{HTTP_REFERER} !.abc.com.$
RewriteRule ^(.*)$ www.xyz.com/$1 [L,R=301]
Says if the referer is not ABC.com then redirect.
Not tested but should work as the 301 will include the referer
I have an ip list file for my country, as like this (txt document):
45.123.116.0/22
5.2.80.0/21
5.11.128.0/17
5.23.120.0/21
5.24.0.0/14
etc
i have two question about that.
1- can i forward the user, if he is in that list via .htaccess file? (if he is, use this adress.. if not this adress)
2- how can i check 'if the user is in my country' via PHP? i mean, how can i say something like that..
if (strstr('list.txt',$_SERVER['REMOTE_ADDR']))
*1).htaccess file
The visitor blocking facilities offered by the Apache Web Server enable us to deny access to specific visitors, or allow access to specific visitors. This is extremely useful for blocking unwanted visitors, or to only allow the web site owner access to certain sections of the web site, such as an administration*
ErrorDocument 403 /specific_page.html
area.*
order allow,deny
deny from 255.0.0.0
deny from 123.45.6.
allow from all
When using the "Order Allow,Deny" directive the requests must match either Allow or Deny, if neither is met, the request is denied.
doc 1)http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#order
doc 2)http://www.htaccess-guide.com/deny-visitors-by-ip-address/
2) Proof of Concept (can't say this works as is....)
$current_ip = $_SERVER['REMOTE_ADDR'];
$valid_ip = false;
// Convert IPs to Regex
foreach($cfg['ipallowed'] as $index=>$ip);
{
$ip = str_replace('.', '\\.', $ip);
$ip = str_replace('*', '[0-9]|^1?\\d\\d$|2[0-4]\\d|25[0-5]');
if (preg_match($ip, $current_ip)
{
$valid_up = true;
break;
}
}
if ($valid_ip)
1; you can redirect by IP to a holding page:
# Redirect a user to /specific_page.html based on their IP address.
RewriteCond %{REMOTE_ADDR} ^10\.0\.0\.2$ [OR]
RewriteCond %{REMOTE_ADDR} ^127\.0\.0\.2$
RewriteCond %{REQUEST_URI} !specific_page\.html$
RewriteCond %{REQUEST_URI} !\.(js|png|gif|jpg|css)$
RewriteRule ^ /specific_page.html [R=302,L]
2; see this question/answer which recommends using http://www.hostip.info/use.html.
I'm working with a client who has a site with many subdomains representing different areas covered in his locksmith business. He picks up a lot of traffic from directory websites, and wants to use his domain only as the link on these websites. When someone clicks it, he wants them to be redirected based on a keyword in the referring URL.
For example, a referring Yell URL could be
yell.com/ucs/UcsSearchAction.do?keywords=locksmith&location=Watford%2C+Hertfordshire&scrambleSeed=1311994593
Client wants htaccess or something similar to pick out the keyword 'Watford' from that URL, and redirect to watford.hisbusiness.com accordingly.
This isn't something I've done before and I'm baffled. Research found no clues.
You can check HTTP_REFERER to grab information from the referring URL.
RewriteEngine on
RewriteCond %{HTTP_REFERER} yell\.com/.*\?.*\&location\=(\w+)\%2C\+(\w+)
RewriteRule ^$ http://${lower:%1}.hisbusiness.com/ [R=302,L]
The ${lower:$1} is used to make Watford lowercase. In order for this to work, you'll need to add the following to your httpd.conf or virtual host configuration file:
RewriteMap lower int:tolower
Note: The rule in place above is designed for the domain root (hisbusiness.com) only - that is to say that a request to hisbusiness.com/something won't trigger the redirect. If you'd like it to check for the URI as well, use the following rule instead:
RewriteRule ^(.*)$ http://${lower:%1}.hisbusiness.com/$1 [R=302,L]
To make the redirect permanent and cached by browsers/search-engines, change 302 to 301.
Use Header on PHP using your required conditions:
if(condition 1){
header("Location: http://mywebsite1.com");
}
if(condition 2){
header("Location: http://mywebsite2.com");
}
else{
header("Location: http://mywebsite3.com");
}
You can use [stristr][1] on the if condition.
We have redirected www.old-domain.com to www.new-domain.com as we are not using old site any more.
So, www.old-domain.com/whatever redirects to www.new-domain.com/whatever. But this causes 404 errors as we don't have those pages.
So I tried to redirect everything from old-domain to the homepage of new-domain as:
if (isset($_SERVER['HTTP_REFERER']) && stripos($_SERVER['HTTP_REFERER'], 'old-domain.com') !== false) {
#header("Location: http://new-domain.com/");
exit;
}
This works fine but not with query string in the url.
For example: www.old-domain/whatever/?utm=123 redirects to the www.new-domain.com/?utm=123
Note the query string is preserved in new url which I don't want.
Note that I even used meta tag & Javascript based redirection but case is still the same.
Anyway to fix the error? Really appreciate your inputs. (we are using nginx server)
Thanks
Regards
It is much better to no to involve PHP for this job. This is suited more for .htaccess.
Place this rule in root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://new-domain.com/? [L,R=301]
Trailing ? will strip off existing query string.
Put this in your nginx configuration file.
server {
server_name .old-domain.com;
rewrite ^ http://new-domain.com/ permanent;
}
I need to access a site only from a specific IP address.
Is that possible using PHP.
The project is under development and some people used that and say "The site is not good". So
i like to avoid that kind of things. That's why i need this solutin.
try this:
if ($_SERVER['REMOTE_ADDR'] == "x.x.x.x")
echo "admin";
else
echo "user";
it checks the ip of user and do the action.
I would suggest to use a .htaccess file instead of adding this to your php-code:
RewriteEngine On
RewriteBase /
# IPs that are still allowed to view everything
RewriteCond %{REMOTE_ADDR} !^213.123.39.12$ [NC]
RewriteRule !^(noentry_image.jpg|favicon.ico)$ sorry_stay_out.html [L]
just put the ".htaccess" file into your root-dir of your website. Then everybody will be redirected to the sorry_stay_out.html page, that contains the noentry_image.jpg.
All visitors from the IP that is allowed will see the site as normal. You can repeat the line "RewriteCond %{REMOTE_ADDR} !^213.123.39.12$ [NC]" with different IPs as often as you want, to add additional IPs.
Alternative with just blocking:
order allow,deny
allow from 62.57.16.192
allow from 72.232.56.154
deny from all
You can use $_SERVER variables to check if the source is from the IP you're limiting to. Here are some useful function snippets ($s is just $_SERVER):
function gethostname($ip){
return gethostbyaddr($ip);}
function gethostnamepretty($ip){
$host=gethostbyaddr($ip);
$host=explode('.',$host);
$max=count($host);
return $host[$max - 2];}
function getRawRealIP($s){
if (!empty($s['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$s['HTTP_CLIENT_IP'];
}
elseif (!empty($s['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$s['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$s['REMOTE_ADDR'];
}
return $ip;}
function checkIP($ip){
return long2ip(ip2long($ip));}
function useragent($s){
return $s['HTTP_USER_AGENT'];}