I was wondering if it was possible to force users to access my website from the homepage only.
For example, if the user enters the address mysite.com/subdirectory (which a valid directory) I want them to be redirected to mysite.com/index.html . From there the can navigate to /subdirectory with the hyperlinks on the homepage.
Thanks in advance!
As all you want to do is to ensure your users see some advertising, and you're currently using the standard Apache directory listing, I'd simply substitute a PHP script (as index.php in each subdirectory) that lists the files in a similar way, but inside an HTML page that you can customise with your advertising. Something along these lines, perhaps, where I've adapted this answer and wrapped it in a web page:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Download Page Example</title>
</head>
<body>
<h1>Download Page Example</h1>
<p>Here's some advertising.</p>
<ul class="file-list">
<?php
// Iterate all files in this directory
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo) {
$filename = $fileinfo->getFilename();
// If it's not a hidden "dot file"
if (!fnmatch('.*', $filename)) {
// And put it on the list as a link, encoding the
// filename as a url path safely in the achor attribute
// and as HTML in the text.
echo '<li>' . htmlentities($filename) . '</li>';
}
}
?>
</ul>
</body>
</html>
You could use a $_SESSION variable defined in the homepage and check in other parts of the website if it is defined. If it's not redirect to your homepage.
For example, your homepage could be:
session_start();
if (!isset($_SESSION['homepage']){
$_SESSION['homepage'] = true;
}
// Your homepage code
and any other page should start with:
session_start():
if(!isset($_SESSION['homepage'] || $_SESSION['homepage'] != true){
header("Location: index.php");
}
Anyway, I can't understand why you would want to do this and I think it should be a proper way of doing what you desire without forcing the user to do this.
One method is to require a POST request to access each subpage. If the user accesses the page directly, it will be a GET request instead. On your home page, do something like this for each link:
<form method=post action="/subdirectory">
<input type=submit value="My stuff">
</form>
(You can use client-side JavaScript to re-work those buttons into links which activate the forms, if desired.)
Then on each subpage:
<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
header('Location: /');
die();
}
you should be able to do this using apache's mod_rewrite
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
put this in an .htaccess file in your main directory
Related
I can't wrap my head around this, and it is SO frustrating. I have no idea how I'd be solving my problem.
I use sockets to connect with my NodeJS server, and I use PHP for routing people to the correct pages. When I route someone to the admin page I check if he is allowed to continue, if so I'll redirect him, if not I won't. Now here's the issue, people can just go to mywebsite.com/browser/admin.html and read everything that's in the admin panel (just the layout and stuff). I don't want people to be able and visit my admin panel to see what admins can do and can't do.
Can I do this in any way? E.g. only show the admin panel if they have a certain rank; if they don't have that rank they WILL NEVER be able to visit the admin panel. I'd like to hide the html files and other files somehow.
Routing - .htaccess
RewriteEngine on
RewriteRule ^/browser/admin.html$ index.php?page=admin [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT} !-f
RewriteRule ^(\w+)$ index.php?page=$1 [L,NC,QSA]
RewriteRule ^(\w+)+\/$ index.php?page=$1 [L,NC,QSA]
Routing - index.php
function route(){
global $db;
global $user;
$page = $_GET['page'];
if(!isset($page)) {
header('Location: /home');
}
if($page === 'home'){
loadWebpage('index.html', array());
}else if($page === 'admin'){
if($user['rank'] !== 'admin'){ /* This works but people can still access the files since they are loaded on the frontend with myexample.com/browser/admin.html */
echo 'Access denied.';
return;
}
loadWebpage('admin.html', array())
}
}
route();
I'm sorry if I haven't explained it well, but I hope you understand what I mean; I find it difficult to explain the things I want. I appreciate the help.
I believe there are plenty of ways to go about what you're trying to achieve, but I'll just explain one way of "access-protecting" a certain file.
Create a file in your directory, admin.php. Here, just have some basic code like this:
<?php
$accessKey = $_POST['accessKey'];
if ($accessKey == "MyVeryAwesomeAccessKeyThatNobodyKnows") //Change this to whatever you want
{
?>
<b>This info will only be shown if you're an admin!</b>
<?php
}
else
echo("Invalid access key.<br>");
?>
This code basically checks if the correct access key was sent in, and only if it was, will it load your protected file.
And then, wherever you have to access the admin page, you can put a form or something there like this:
<form method="POST" action="admin.php">
<input name="accessKey" type="password" />
<input type="submit" value="Load Admin Site" />
</form>
The user has to enter in the administrator access key to be granted permission into the protected information page. This way is a bit hackish, but it gets the job done!
I have a multi language site
I want :
exemple.be/nl/
to redirect to
exemple.be/nl/start_nl.php
and
exemple.be/fr/
to redirect to
exemple.be/fr/start_fr.php
Can I do this, possibly with .htaccess?
I tried this, to no avail:
301 /nl/ https://exemple.be/nl/start_nl.php
There is a function called setlocale in php that be of use for making it so that it goes to a specific site with header("Location: start_nl.php");
http://php.net/manual/en/function.setlocale.php
But otherwise you can make so so the index.php goes to the specific site with header("Location: start_fr.php");
Just put in on the top, under the opening tag for php:
<?php
header("Location: start_fr.php");
?>
It can also be done with html.
<meta http-equiv="refresh" content="2; URL=start_fr.php">
I found the answer on another site
In the .htaccess file, I wrote :
RewriteEngine On
RewriteRule ^nl/?$ https://exemple.be/nl/nl-start.php
RewriteEngine On
RewriteRule ^fr/?$ https://exemple.be/fr/fr-start.php
In my current website project I have chunks of php and html code that are repeated throughout the project. For example, every page has the same header and footer as all the others. So, for the sake of modularizing my code I have created separate php files for things like these. Here is an example of my header.php file.
//header.php
<?php
if(isset($_POST['submittext'])) {
echo $_POST['sometext'];
}
?>
<div id="header">
<form action="header.php" method="post">
<input type="text" name="sometext">
<input type="submit" name="submittext">
</form>
<div id="somediv">
<?php
//Some more php
?>
</div>
</div>
And here is an example of a file that contains code for things unique to the page, unlike the header or footer. Call this file home.php.
//home.php
<?php
//Some php
?>
<!DOCTYPE HTML>
<html>
<head>
<!-- Style sheets, etc. -->
</head>
<body>
<?php include('header.php'); ?> //Here I include the above file
<div id="main">
<!-- Html for everything below the header -->
</div>
</body>
</html>
Now the problem is that I have a form in the header.php file. So when I submit the form, the action attribute says to reload header.php. But if it does that, then the other code in home.php doesn't load such as the "main" div. So my question is, how can I modularize my code to prevent very long and hard to read php files, while also being able to load all my content? My first idea was to just put header('Location: home.php'); after I process the form in the header.php file, but then if I include the header.php file in another page, say home2.php, it will redirect me back to home.php rather than home2.php.
By using a simple pure php router, you can load multiple views in a modular sense that you might be looking for by simply calling routes on your site. So example.com would fall under the null route, and you could have your views render there, and then you could have a example.com/home2 route which would display all your views for that particular route.
In doing this, you can then tell your post.php page to redirect to different routes and render all the modular component views you may need for that page.This method also cleans up the URL and shows /home instead of home.php.
Put this code in your index.php file.
$site_url = 'www.example.com';
function getCurrentUri()
{
$basepath = implode('/', array_slice(explode('/',
$_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
$uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?'));
$uri = '/' . trim($uri, '/');
return $uri;
}
$base_url = getCurrentUri();
$routes = array();
$routes = explode('/', $base_url);
foreach($routes as $route)
{
if(trim($route) != '')
array_push($routes, $route);
}
switch($routes[1]) {
case('home2'):
$page = 'home2';
include('header.php');
include('home2.php');
include('footer.php');
break;
case(null):
$page = 'home';
include('header.php');
include('home.php');
include('footer.php');
break;
default:
header('Location: http://'. $site_url .'/404');
break;
}
You might also need a .htaccess file in your base directory to allow for the rewrites so try this
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
But don't quote me on that ^^ htaccess syntax is confusing.
Also sidenote, it would be better practice to put the full <html><head> and start of the <body> tag in the header.php file instead of again rewriting that code for your other pages when you could just include a full html head.
So essentially this would become your header.php
<!DOCTYPE HTML>
<html>
<head>
<!-- Style sheets, etc. -->
</head>
<body>
<?php include('header.php'); ?> //Here I include the above file
Which very well could solve your problem, if you then redirected to home.php and at the top before your home page code you included the header.php
I want to change my address bar via htaccess.
My link is: http://englishforyou.ir/index.php?content=about
And I want to change it to http://englishforyou.ir/content/about.My htaccess code is:
RewriteEngine on <br>
RewriteRule ^content/([A-Za-z0-9-]+)/?$ index.php?content=$1 [NC]
But it doesn't work. You can test it here: englishforyou.ir
My index file is:
<?php
require_once 'includes/function.php';
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div>
test
About me
</div>
<?php loadContent('content', 'mainpage'); ?>
</body>
</html>
My function file is:
function loadContent($where, $default='') {
// Get the content from the url
// Sanitize it for security reasons
$content = filter_input(INPUT_GET, $where, FILTER_SANITIZE_STRING);
$default = filter_var($default, FILTER_SANITIZE_STRING);
// If there wasn't anything on the url, then use the default
$content = (empty($content)) ? $default : $content;
// If you found have content, then get it and pass it back
if ($content) {
// sanitize the data to prevent hacking.
$html = include $content.'.php';
return $html;
}
}
And my about me file is:
<div>
<h4>
This page is about me.
home
</h4>
</div>
Your .htaccess file is right and it is working.
You might have to refresh your browser without cache, or wait for it to refresh by itself.
If you are using Firefox or Chrome, use Ctrl+F5 to refresh overriding the cache.
Note: You just have to use the url http://englishforyou.ir/content/about instead of http://englishforyou.ir/index.php?content=about
EDIT
Add this line to your .htaccess to redirect from http://englishforyou.ir/index.php?content=about to http://englishforyou.ir/content/about
RewriteRule ^index\.php\?content\=(.*)$ /content/$1 [R=301,L]
Try something like this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L]
RewriteRule ^system/([^/]*)\.html$ system.php?route=$1
This translates the url http://www.domain.com/system/home.html to http://www.domain.com/system.php?route=home
And make sure you are allowed to use .htaccess
I have some trouble with my cms. (My own cms)
If I write www.example.com/example It works fine but if I write www.example.com/example/ the /example's content dosent show. Only the header, navigationbare, sidebar and footer shows up.
I need to write php code in site_content (In the mysql table) currently I can only write html)
It seems that google's spiders cant find my pages. Any way I can create a sitemap for google (The sitemap needs to get updated automaticaly when I add a new page in the mysql table (sites)
Structure of mysql
DataBase: website
Table: Sites
site_id
site_link
site_title
site_content
My index.php
<html>
<?php
include_once 'includes/mysqlconnect.php';
$url=$_SERVER['REQUEST_URI'];
$query = mysql_query("SELECT * FROM sites WHERE site_link = '$url' OR site_link = '$url/'");
WHILE($rows = mysql_fetch_array($query)):
$site_id = $rows['site_id'];
$site_link = $rows['site_link'];
$site_title = $rows['site_title'];
$site_template = $rows['site_template'];
$site_content = $rows['site_content'];
endwhile;
?>
<head>
<meta charset="UTF-8">
<title>KasCraft | <?php echo $site_title;?></title>
<LINK rel="stylesheet" type="text/css" href="http://kascraft.com/themes/default.css">
</head>
<body class="body">
<div class="container-all">
<div><?php include_once 'includes/header.php';?>
</div>
<div class="container">
<div><?php include_once 'includes/navigationbar.php';?></div>
<div><?php include_once 'includes/rightsidebar.php';?></div>
<div class="content">
<?php echo $site_content;?>
</div>
<div>
<?php include_once 'includes/footer.php'; ?>
</div>
</div>
</div>
</body>
</html>
And my .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Can anybody please help me??
It could be that your .htaccess is only looking at the root directory of the site. /example/ is a new directory. I use a similar script that does include subdirectories, maybe you can use this:
RewriteEngine On
#In case of these folder, halt and don't continue.
RewriteRule ^(images|javascripts|styles)($|/) - [L]
#In every other case (exept these extentions) open index.php.
RewriteRule !.(gif|htm|html|ico|jpg|jpeg|js|png|txt|xml)$ index.php
With 'write php code' I assume you mean saving php code to the database? That works in the same way as saving html code. However when you're loading it from the database again you can choose to display it as text or execute it as PHP code using eval(). Personally I'd look at including templates in PHP. Have a look at this: https://stackoverflow.com/a/19603372/3079918
How long ago did you submit your website? It can take a few weeks before Google picks it up. If you really need a sitemap, build a script that will load your pages from the database and build them into a .xml file. Have a look at http://www.google.com/webmasters/ on how to submit the sitemap to Google.