Changing address bar via htaccess - 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

Related

Force users to access website from the homepage

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

I have trouble with my php/html/mysql cms

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.

Displaying dynamic content based on browser location

I had an idea of writing a page template in my index.php where the content section of the page will include the content dynamically by determining what url the browser is at and getting the content page based on that. There's a few issues though:
<?php
// /index.php
?>
<!-- html tags and menu/layout divs as needed go here -->
<section id="content">
<?php
function curPageURL()
{
$page = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
return $page;
}
$page = curPageURL();
switch ($page)
{
case "index.php":
include_once("includes/home.php");
break;
}
?>
</section>
<!-- footer and such goes here -->
Although this works, I'm having trouble applying the concept further. I just can't seem to get my head around how the navigation should work or how to apply this concept to other pages in my site...
If anyone can advise, I'd really appreciate it.
You could use a .htaccess file (put in the same directory as index.php).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?uri=$1 [L,QSA]
And then in index.php you can get the page with:
$page = $_GET['uri'];
So if you user goes to example.com/home then the page variable will be set to home.

.htaccess rewrite error when using more than one parameter

I have this in my .htaccess :
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . /index.php
and this is how i work with the parameters :
<?php
$request = $_SERVER['REQUEST_URI'];
$request = substr($request,1);
$params = explode('/', $request);
$safe_pages = array('page1','page2','page3');
if(in_array($params[0],$safe_pages))
{
include($params[0].'.php');
}else
{
include('404.php');
}
?>
Let's say my site is : www.site.com
www.site.com/page -> works perfectly
www.site.com/page/parameter -> doesn't load any css or images because it looks for them in root/page instead of root/
I tried to use absolute links for my css and images but it still didn't work, so i guess the problem is in the .htaccess or i'm missing something.
The problem lies not within htaccess rewrite rules, but in your html - yuor links to static content (like css, images or scripts) should start with slash to be ralative to the domain. So instead of
<link href="css/style.css" />
<script src="js/script.js"><script>
Use
<link href="/css/style.css" />
<script src="/js/script.js"><script>
Other way is to use base tag but it can break named anchors on your page
Use
<base href="http://absolute_path_here/">
in your HTML before the closing </head>.

Invalid controller specified (css) in Zend framework

I am trying to apply css in the Zend Framework. My css file is in public/css/layout.css. First of all I added the following code lines in bootstrap.php:
protected function _initPlaceholders()
{
$this->bootstrap('View');
$view = $this->getResource('View');
$view->doctype('XHTML1_STRICT');
// Set the initial title and separator:
$view->headTitle('My Site')
->setSeparator(' :: ');
// Set the initial stylesheet:
$view->headLink()->prependStylesheet('../../css/layout.css');
}
Which simply specifies the title and stylesheet to be used. Then in layout.phtml I add the following code:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
echo $this->headTitle();
echo $this->headScript();
// add a link to the site style sheet
echo '';
echo $this->headLink();
?>
</head>
Which simply adds link of the stylesheet specified in bootstrap.php.
But when I run the project then I am getting the form without css. Why is this? When I check the css with Firebug in a Mozilla browser it says:
<h3>Exception information:</h3>
<p>
<b>Message:</b> Invalid controller specified (css) </p>
<h3>Stack trace:</h3>
<h3>Request Parameters:</h3>
<pre>array (
'controller' => 'css',
'action' => 'layout.css',
'module' => 'default',
) </pre>
Please help me out to resolve this issue.
Try
$view->headLink()->prependStylesheet('/css/layout.css');
this should help.
Do you have this code on your /public/.htaccess file?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
If you don't have the first 4 lines, this file will redirect your request of "css/layout.css" to index.php and then, Zend will interpret it as a link for a controller -> action.
If you have this file, make sure that mod_rewrite is enabled on your server.
And you sould put your link as "akond" said.
Good luck =)
This helped me:
Zend_Controller tries to execute my stylesheets as controller
[update]
Ok, just added the following line to my .htaccess file and all works as expected now...
RewriteRule !.(js|ico|txt|gif|jpg|png|css|htc|swf|htm)$ index.php
but then I found this one:
http://devzone.zend.com/1651/managing-css-and-javascript-files-within-a-zend-framework-app/

Categories