I need to hide the profile.php from appearing in the URL.
So here's what I have:
The URL I am accessing:
sampleuser.domain.com/profile.php
How I get the user 'sampleuser':
$url = "//".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
/*this will give me $url = '//sampleuser.domain.com/profile.php'; */
function get_user_from_url($url)
{
if (strpos($url , 'www.') !== false) {
preg_match('/\/\/www.(.*?)\.domain/', $url, $val);
}
else
{
preg_match('/\/\/(.*?)\./', $url, $val);
}
return $val[1];
}
$user = get_user_from_url($url);
echo $user;
I just need to hide the profile.php, also the thing to consider is if we hide the profile.php, it might conflict with the index.php.
But luckily we will only access the index.php if the URL is only domain.com (without user).
I'm low on htaccess knowledge, can anyone give me the direct answer, and I would be very appreciative if you can add some explanation, it'll give me a head start on learning htaccess.
So basically, what I was making is a dynamic subdomain. So I added a subdomain on my DNS:
*.domain.com
then added these on my htaccess
<IfModule mod_rewrite.c>
#Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule ^/?$ /agent/index.php?user=%2 [L]
</IfModule>
Related
I've got CloudFlare running on my clients WordPress site. In CloudFlare, I have IP Geolocation on along with IPv6 Compatibility and the Psuedo IPv4 on as well.
What I'm trying to do is get anyone from a Canadian IP address to redirect to the Canadian version of my site. I tried this code in the header.php of my child theme:
$country_code = $_SERVER ["HTTP_CF_IPCOUNTRY"];
if ($country_code=="CA") {
$link = 'https://ca.example.com';
}
else {
$link = 'https://example.com';
}
header("location:$link");
exit;
But that did created redirect errors on the US side. It did seem to redirect properly in Canada. So how can I make it work where Canadians get the redirect but no one else does?
I also tried this in .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
SetEnvIf CF-IPCountry "(.*)$" Country=$1
RewriteCond %(ENV:Country) CA
RewriteRule ^(.*)$ https://ca.example.com/$1 [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Now that didn't cause any errors but it also didn't redirect either.
Paste this in the functions.php file of your child theme. Worked for me.
add_filter( 'after_setup_theme', 'ip_redirect_on_cloudflare', 1);
function ip_redirect_on_cloudflare()
{
$CFCountry = $_SERVER['HTTP_CF_IPCOUNTRY'];
switch ($CFCountry){
case "CA":
wp_redirect( 'https://ca.example.com' );
exit;
}
}
Use wp_redirect() function with template_redirect hook https://developer.wordpress.org/reference/functions/wp_redirect/
My current link is:
domain.com/folder/index.php?page=home
and i try to get it to this:
domain.com/folder/home
The PHP code that I use to get the page
<?php
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 'home';
}
if (strpos($page, '/') !== false || !file_exists("pages/$page.php")) {
$page = 'error';
}
include ("pages/$page.php");
?>
I tryed a lot to get the right .htaccess code to rewrite my URL, from generators to forum posts but I can not get the right code.
I hope someone can get me in the right direction to get this working.
these .htaccess rules should help you get started
RewriteEngine On
RewriteBase /
#### this allows you to write your links in the form /folder/home ####
RewriteRule ^folder/([0-9]+)$ folder/index.php?page=$1 [NC,L]
#### to dynamically redirect to /folder/home ####
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?page=([^&\s]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
Redirect ^(.*).php$ index.php?page=$1 [L,QSA]
utilize this in your htaccess
I have a web site where I have to use a .htaccess file to redirect all requests to index.php, where redirecting is handled.
I want to rewrite the URL, and at the same time use HTTPS. Without HTTPS it works fine.
Code from working .htaccess without HTTPS. Browser gets this input: alert/create
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
This works fine, but without HTTPS. Browser URL becomes http://localhost/mypage/alert/create, and that's what I want.
I found a solution that allows me to use HTTPS:
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ https://%{SERVER_NAME}/mypage/index.php?controller=$1&action=$2&id=$3 [NC,L]
Page navigation works like a charm, but browser displays the URL like this:
https://localhost/mypage/index.php?controller=alert&action=create&id=
Requests are handled like this:
public function __construct($urlvalues) {
$this->urlvalues = $urlvalues;
if ($this->urlvalues['controller'] == "") {
$this->controller = "home";
} else {
$this->controller = $this->urlvalues['controller'];
}
if ($this->urlvalues['action'] == "") {
$this->action = "index";
} else {
$this->action = $this->urlvalues['action'];
}
}
I need some hints. I've been looking all over internet without solving my problem...
If I can use .htaccess, but implement HTTPS another way, that'll be perfect too.
Server code written in PHP, running on apache2.
I would have done this in two steps. The first that you already have. And this one :
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
Solved! Solution: Added this to .htaccess, in that order spesifically:
RewriteEngine on
#force https
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
I want to protect pages from the actual path so I'm using the server uri variable to know what the user write in the nav bar:
page.php
if ($_SERVER['REQUEST_URI'] == '/path/to/page.php') {
unset($_SERVER['REQUEST_URI']); // nothing will be displayed
} else // page content
And it's working fine, but now the problem is ?id=x or just adding ? will show the page with errors.
Is there a way to add OR == ?....
I want to prevent the direct access, because I'm using a router that includes those pages in index.php like this: site.com/page and site.com/page?...
Thank YOU!
EDIT: Add more info:
.htaccess
Options -Indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
the router in index.php
// array whitelist for match
$includes = array(
'/home' => 'dir/to/home.php',
'/other' => 'dir/to/other/page.php'
);
if ($_SERVER['REQUEST_URI'] == '/')
$_SERVER['REQUEST_URI'] = '/home';
preg_match('/^([\w\/]+)/', $_SERVER['REQUEST_URI'], $matches);
$matches[1] = isset($matches[1]) ? $matches[1] : null;
if(array_key_exists($matches[1], $includes)) {
$content = include($includes[$matches[1]]);
} else $content = include('views/error.php');
return $content;
I don't know if I understand, but if you do this way:
if (preg_match('\/path\/to\/page\.php', $_SERVER['REQUEST_URI'] ))
I have many articles in foo.com/articles/
like this:
foo.com/articles/banana.php
foo.com/articles/strawberry.php
and want redirect SEO to subdirectory like this:
banana.foo.com
strawberry.foo.com
EDIT
when I type (banana.foo.com) show me (foo.com/articles/banana.php).
just show and not realy redirect
I can not create many subdomain by Cpanel, therefore (banana.foo.com) do not create by Cpanel
put this in your .htaccess file ( don't forget to replace "yourdomain" with your domain name)
Options +FollowSymLinks
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*).yourdomain.com$
RewriteRule (.*) yourdomain.com/articles/%1.php [L]
Now when you request
banana.foo.com
The result will come from
foo.com/articles/banana.php
and so on
You might do something like:
if (preg_match('#.*/articles/([a-z0-9\-_]+)\.php#i', $_SERVER['PHP_SELF'], $match)) {
header('Location: http://' . $match[1] . '.foo.com');
exit;
}
Remember to paste this code before any other output or you'll get an error.
Or via .htaccess with mod_rewrite enabled and RewriteEngine ON:
RewriteRule ^.*/articles/([a-z0-9\-_]+)\.php$ http://$1.foo.com [R,L]
EDIT
The reverse would be something like:
if (preg_match('#^http://([a-z0-9\-_]+)\.foo\.com.*$#i', $_SERVER['HTTP_HOST'], $match)) {
header('Location: http://foo.com/articles/' . $match[1] . '.php');
exit;
}
And with .htaccess:
RewriteCond %{HTTP_HOST} ^([a-z0-9\-_]+).foo.com$
RewriteRule .* http://foo.com/articles/%1.php [R,L]
In .htaccess you can do this with the following:
RewriteRule ^foo.com/articles/([a-z0-9-]+).php $1.foo.com [PT,L]
You'll need mod rewrite installed, and you'll need a wild card subdomain set up in your DNS server. http://en.wikipedia.org/wiki/Wildcard_DNS_record