I have many hours trying to find the solution here but none looks like what I'm looking for ... I will give a long explanation and possibly someone will serve you what I have and not need to change it.
Table pages
Table products
File .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?route=$1&url=$2 [NC,L]
If I put in the browser: example.com/pages/contact
really we are in: example.com/pages.php?route=pages&url=contact
In the file pages.php
<?php require_once 'inc/header.php'; ?>
<section>
<div class="container">
<div class="row">
<?php
$route= $_GET["route"];
$url = $_GET["url"];
$results = DB::query("SELECT * FROM $route WHERE url='$url'");
foreach ($results as $row) { ?>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<h2><?php echo $row['name']; ?></h2>
</div>
<?php } ?>
</div>
</div>
</section>
<?php require_once 'inc/footer.php'; ?>
The same goes for products.php, is the same code as all get by _GET
The problem I have:
I want to eliminate the $route in the URL, I want the URL looks like this:
example.com/contact or example.com/acer-one
Is how to do this, if the two designs are identical, ie, put everything in index.php, the problem is I need to use the two files to display a design or another.
Where do I begin? I modified it? It is a project I'm starting from scratch, so I can change anything.
Have a look at the following example structure:
Start by adding a table "routes" that just contains each possible url, along with a type (and every field that both pages and products have in common). This would also mean you can remove the url column (and the other common columns) from the pages and products table (or perhaps remove the table all together, if no relevant data is left). Something like this:
TABLE routes
id route type name
1 contact page Contact
2 acer1 product Acer 1
Add your main controller, probably index.php, that looks something like this:
<?php
$url = isset($_GET["url"]) ? $_GET["url"] : 'home';
// you should probably use prepared statements here, but that is a different topic
$result = DB::query("SELECT * FROM routes WHERE url='$url'" LIMIT 1);
if (! $result) {
// 404
}
// any other common logic can go here, like header and stuff
require_once 'inc/header.php';
// now load the type specific page
include $result['type'] . '.php';
// and continue with the shared logic
require_once 'inc/footer.php';
Change your .htaccess to send everything to index.php
RewriteRule ^(.*)$ index.php?url=$1 [NC,L]
Now you only need a single path element and no type in the url. And you could add a page with url home for when no path elements are provided. Plus you have the possibility to still work with deep links 'my/awesome/deep/linked/page', all you have to do as add that url to your routes table. Inventing a new page type would be very easy as well, just add a 'my-new-type.php' file for the custom logic and you are set.
The key is to have a single point of entry into your application, a so called frontcontroller. And keep your code DRY, Don't Repeat Yourself.
Related
hello stackoverflow community hopefully you understand what im trying to do
im working with pretty URLs with htaccess and php and i have seen in some pages that in the URL appear the title of the news something like...
http://www.samplePage.com/news/killer-shooter-in-new-york
i would like to know how i do that , im not that good in htaccess or friendly URLs but i try to do my best and this is the way that im doing it....
my .htaccess
Options All -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9/]+)$ index.php?view=$1
my html
//after the header label i add this to include the content
<?php
if (isset($_GET["view"])) {
$view=explode("/",$_GET["view"]);
include"modules/".$view[0]."-view.php";
}else{
include"modules/home-view.php";
}
?>
now i got these links that take me to the news
<ul class="row news-list">
<?php $sql=$con->query("SELECT * FROM latest_news LIMIT 4");
while ($row=$sql->fetch_array()) { ?>
//note: the URLSERVER is the route for example http://localhost/projectNews/
<li class="grid_6">
<?php echo $row['post_title']?>
</li>
<?php } ?>
</ul>
the ouput of this is http://localhost/projectNews/news/4/
once i click the link what i would like to do is to add the title of the new after the id of the news like i explained before for example...
http://localhost/project/news/4/killer-shooter-in-new-york
can someone of you guys help me please? im a little bit of new on this, thanks!
Okay, first; I'm relatively new to php.
I know the basics but nothing too fancy.
I'm developing a website where the header and the footer stay the same on which ever page you are.
But the problem is this (obviously).
The index.php looks like this:
<?php include("header.php") ?>
// Body content
<?php include("footer.php") ?>
In the header.php there's also the < head> tag.
When I change a page that is in an underlying folder, the css path ofcourse stays the same (so without the "../" ).
And there's my problem, is this fixable or do I have to exclude the < head> tag from the header.php?
Thank's in advance.
The easiest way to solve this is to use absolute paths like this:
/path/to/css/file.css
If the header page stays always in the same directory, I think that there is no problem with that.
In the pages that are in the other folders you just have to point to where the header is and then the header will be in the right directory to call the css's.
Like:
<?php
include("header.php")
?>
And in a subfolder:
<?php
include("./header.php")
?>
Have you tried this?
I suggest to do a different approach.
Any request is handled by one file, so you index.php
in there you have
<?php include("header.php") ?>
// Body content
<?php include("footer.php") ?>
you can then create your directory structure and catch the script via an URL $_GET parameter
e.g. index.php?request=page1
you can then handle this in PHP saying
switch ($_GET['request']){
case 'page1' : require './sub/page1.php';break;
....
default: require 'real_index.php';break;
}
Then you can make your SEO-urls by using the .htaccess file. This is basically a rerouting of the url (mod_rewrite in Apache)
There you should have something like this:
RewriteEngine On
RewriteRule ^(.+)$ index.php?request=$1 [QSA,L]
So any request is then redirected, to the script you want, but on the server side you always stay on the same directory level like index.php is.
e.g http://localhost/page1 is then redirected to http://localhost/index.php?request=page1
For a bit more explanation maybe this video helps BasicMVC tutorial
Its a bit fast for beginners, but very good explained.
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.
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.
I have the following code:
<html>
<head>
<title><?php echo $GLOBALS['L']['title']; ?></title>
</head>
<body>
<ul id="language-selection">
<li>English</li>
<li>French</li>
</ul>
<h1><?php echo $GLOBALS['L']['h1']; ?></h1>
<p><?php echo $GLOBALS['L']['p1']; ?></p>
<ul id="language-selection">
<li>About Page</li>
<li>Contact Page</li>
</ul>
</body>
</html>
set_locale.php:
<?php
/*
* File: set_locale.php
*/
// Get the language from the query string, or set a default.
($language = #$_GET['lang']) or $language = 'english';
// Set up a list of possible values, and make sure the
// selected language is valid.
$allowed_locales = array('english', 'french');
if(!in_array($language, $allowed_locales))
$language = 'english'; // Set default if it is invalid.
// Inlclude the selected language
include "locale/$language.php";
// Make it global, so it is accessible everywhere in the code.
$GLOBALS['L'] = $locale;
?>
It works OK, but if I click the about.php and contact.php link.
The page returns to the default language: English.
What can I do so that when I click about.php or contact.php ends up like this:
about.php?lang=english
contact.php?lang=french
respectively, in other words I want the URL to remember the ?lang= ending.
What's the best way of doing it?
You'll have to append it to every outgoing link:
<li>About Page</li>
a nice way of dealing with multi-language sites in general is, if your server supports it, mod_rewrite to rewrite "virtual" URLs like
www.example.com/en/about.php
and map them internally to
www.example.com/about.php?lang=en
there's a beginner's guide on that here and official documentation here.
I'm no mod_rewrite guru but this works for me:
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_URI} ^/([a-z][a-z])(/.*)?$
RewriteRule (.*) %2?lang=%1&%{QUERY_STRING}
it maps
www.domain.com/en/about.php to /about.php?lang=en
www.domain.com/fr/about.php to /about.php?lang=fr
www.domain.com/es/ to /?lang=es = usually index.php
It maps any occurrence of a two-letter, lowercase www.example.com/xy, so you shouldn't have any directories with two letters on your root level to work with this.
You will want to learn about storing information in sessions.
http://learnitscreencasts.net/2009/07/18/beginners/beginners-guide-php-sessions/
You might want to look into sessions and store the persistent options there. It has the advantage of allowing people to copy links to others without forcing their settings upon them, if you so desire such.
You could use the output_add_rewrite_var to add that argument to the URLs. Just call the following before you output your contents:
output_add_rewrite_var('lang', $language);