Creating A blog with PHP from scratch using htaccess [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Creating a blog from scratch without using any PHP framework, the blog will take a folder inside my domain root like this domain.com/blog
How could i make an htaccess file that makes my blog posts take links like this :
domain.com/blog/blog-post-test-link-foo-bar
Instead of this way :
domain.com/blog/post.php?id=1
Also, In my post.php file, i want to select the data of that post similar to the Slug in the url.
How could i do that in pure php way?

You must create the /blog/ folder and inside it you can put an .htaccess file like the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . ./index.php [L]
</IfModule>
Then in your index.php you would write something like:
<?php
$base = '/blog/';
$uri = $_SERVER['REQUEST_URI'];
$slug = substr($uri, strlen($base));
// Identify the ID of the post
// You identify the ID by searching your posts database on a field like "slug"
// But for now we'll just load from an array
$posts = array(
'blog-post-test-link-foo-bar' => 1
);
$postId = isset($posts[$slug]) ? $posts[$slug] : null;
if (!is_null($postId)) {
// Load the post from the database,
// or use it if you already loaded it together with the ID
echo "Loading!";
} else {
echo "Invalid post!";
}

Related

change php dynamic pages url [duplicate]

This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
pretty urls from database
(2 answers)
Closed 5 years ago.
I just started learning php and i created a simple php website which contains posts, users. and i made posts links looks like : http://localhost/?post=3
my question is there any way to make the url looks differents like http://localhost/post/252155 without creating php pages for it.
Here is my code snippet
$results = mysqli_query($conn, "SELECT * FROM projects");
$project = array();
if (mysqli_num_rows($results) > 0) {
while($row = mysqli_fetch_assoc($results)){
$project[] = $row;
}
}
$projects = 0;
$maxprojects = 14;
while ($projects < $maxprojects) {
$projectId = $project[$projects]["project_id"];
$projectURL = "../basic/?project=".$projectId ;
$projectTitle = $project[$projects]["project_title"];
$projectThumb = $project[$projects]["project_thumbnail"];
$projectPrice = $project[$projects]["project_price"];
echo "
<li class='gig'>
<div class='gig-content'>
<span class='thumbnail'>
<a href='$projectURL'><img src='".$projectThumb."'></a>
</span><!-- Gig Thumbnail -->
<span class='title'>
<h2><a href='$projectURL'>".$projectTitle."</a></h2>
</span><!-- Gig Title -->
<div class='meta'>
<span class='price'>
<h1>".$projectPrice."$</h1>
<h4>Startin at</h4>
</span><!-- price -->
</div>
</div>
</li><!-- Gig -->
";
++$projects;
}
thanks in advance!
To accomplish this, you need to first create a text document called ".htaccess" to contain your rules. It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). There may already be a .htaccess file there, in which case you should edit that rather than overwriting it.
RewriteEngine On
RewriteRule ^post/([0-9]+)/?$ ?post=$1 [NC,L]
NOTE: Only If Application runs on Apache
A common approach is for you to have the web server route all URL requests to any arbitrary (so long as an actual resource–aka file–does not exist at that location). With the Apache web server, you can use create a .htaccess to route all URLs for which no file or directory exists, to your index.php file. For example, at the root of your publicly facing directory, its .htaccess could contain
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This will route all URLs that do not exist as a file or directory to index.php in the root directory for the website. (This is the way that WordPress does it).
Then, within your index.php, you can parse the URL and serve up whatever content you wish base on the URL (or not). You can create your own routing system, within your PHP code, to direct content generation to whichever PHP page you prefer.
<?php
print_r($_SERVER['REQUEST_URI']);
For example, https://yourdomain.com/post/13452345 would output, /post/13452345.
%_SERVER['QUERY_STRING'], parse_url() and might also be useful. And installation/configuration dependent values might also be available: $_SERVER['SCRIPT_URI'] and $_SERVER['SCRIPT_URL']
More information can be found at the PHP reference site Predefined Variables, $_SERVER
You can parse the URI as you wish:
$URI_COMPONENTS = explode( '/', $_SERVER['REQUEST_URI'] );
if ($(URI_COMPONENTS[0] == 'post') {
// get $(URI_COMPONENTS[1])
// output response based on post value
// Maybe something like put_post_content($(URI_COMPONENTS[1]));
}
else
{
http_response_code(404); // Not a "post" request, so invalie URL
}

from request $ _GET to folder [duplicate]

This question already has answers here:
.htaccess rewrite GET variables
(7 answers)
Closed 4 years ago.
I have a website with a dynamic structure: for every $_GET['action'] I include a file with a different content, while the footer and the header always remain the same. example: www.mysite.com/index.php?action=service1
I would like to change this "structure" in a way:
www.mysite.com/service1/ is there a way to include the $_GET['action'] request in every folder I create?
The site in question has over 100+ $_GET['action'] and creating a page for each action would become heavy .
any advice on how to do?
You can set up your .htaccess to pipe certain values to your $_GET variables.
Learn more here.
The basic idea is to add something like:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^service/(\d+).*$ ./myActionscript.php?id=$1 [L]
Then all requests you send to /service/{number} will go to your file myActionScript and you can do what you want with that {number} there by using string manipulation on the $_SERVER['REQUEST_URI'] to get it.

How remove .php extension from codeigniter? [duplicate]

This question already has answers here:
CodeIgniter removing index.php from url
(35 answers)
Closed 5 years ago.
My link show this:
http://localhost/project/index.php
I want this:
http://localhost/project/
Do not display extension
You don't understand MVC pattern, there is no direct access to files within URL, everything is redirected to one page.
Full URL: http://localhost/project/home/about
root: http://localhost/project
controller: home
method inside given controller: about
Read more about MVC in Codeigniter's website: https://www.codeigniter.com/user_guide/overview/mvc.html
Or here: https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
CodeIgniter provide Routing rules/URI Routing for this.
You can achive this by adding variable to $route array in
/application/config/routes.php file
$old_path = 'home/about'; // no need to add .php to about
$new_path='home/about'; //or about or any other name you want
$route[$new_path] =$old_path;
Now you can visit page by only
http://localhost/project/home/about
For more details :-
https://www.codeigniter.com/user_guide/general/routing.html?highlight=route
Codeigniter's mvc works as project_folder/index.php/controller/function.
If you modify this behaviour with htaccess it might collapse the behaviour of the framework.
Learn how it works and get clarified. This link might help you.
Create a file with the name ".htaccess" just ".htaccess" NOT "test.htaccess" or so.
Write this code in it ->
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Now you can link your sites like this -> Test

How to write a php code so that I can create a link like this "http://website/2017/name.html" [duplicate]

This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
Closed 5 years ago.
I'm sorry that my question itself is not the way it should be. I am new to php and the links to open a post ( let's take that I am creating a website something like a blog ) is like open.php?id=sth
I have seen in blogger and many other sites the links to open a post is in the type I have given in the question head. I would like to know how this is done. Is this in a way by which they create a directory and make an html file (as in the example) in that particular directory?
You can do that with a ".htaccess" file and apache url rewrite module.You don't need to create a directory.
Create a ".htaccess" file on the root folder and add something like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^?]*) ./open.php?params=$1 [L]
This will make your url like "http://example.com/2017/name"
You can debug your parameters with that code:
var_dump($_GET);
open.php?url=http://example.com
<?php
header('Location: '+urldecode($_GET['url']);
?>

PHP. Handle every url on website [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want every user request(url) to handle in index.php (or some another file). I need to handle these requests to check if requested page exists(in database) and then construct the page . Using this approach I can store content not in the server itself, but in the database. I suspect that Joomla cms works this way. I am right? Can you post sample code?
(user must see full url he typed in browser's adress bar, and when he click on the backward arrow in browser, he must get the previous page - I suppose i can't use redirect and multiple requests)
There is a design pattern for it called Front Controller Pattern, which provides a centralised entry point.
To achieve this in PHP, you may use any MVC framework like Codeignitor which works in FCP by default.
To achieve this in PHP, first have your .htaccess modified.
RewriteEngine On
RewriteRule . /index.php [L]
And in your index.php, have a logic like this:
index.php (code simplified for understanding)
<?php
if ($_SERVER['REQUEST_URI'] == '/about') {
// Logic for printing About
getContent('about');
} elseif ($_SERVER['REQUEST_URI'] == '/products') {
getContent('products');
} else {
// Logic for printing 404 page
}
function getContent($key) {
// load value based on $key
//print content.
}
Try this in .htaccess
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [NC]

Categories