How to solve htaccess url problems - php

I am a new member in stackoverflow and beginner for php. I started one sample blog project. It works fine, but I want to change url for seo friendly. Can any one help me please? How to write htaccess code for this blog.
index.php
<?php
include_once("db.php");
$sql = mysql_query("SELECT * FROM post");
?>
<html>
<head>
<title>
Post
</title>
</head>
<body>
<h1>Post List</h1>
<ul>
<?php
while($row = mysql_fetch_array($sql)){
?>
<li><?php echo $row['title']; ?></li>
<?php }?>
</ul>
</body>
</html>
post.php
<?php
include("db.php");
if(isset($_GET['pid'])){
$id = $_GET['pid'];
$qry = mysql_query("SELECT * FROM post WHERE id=".$id);
}
if($qry === FALSE) {
die(mysql_error()); // TODO: better error handling
}
?>
<html>
<head>
<title>
View Post
</title>
<style>
body{
background-color: #c9c9c9;
}
h1, .para{
border: 2px solid red;
padding:10px;
}
</style>
</head>
<body>
<?php
while($row1 = mysql_fetch_array($qry)){
?>
<h1><?php echo $row1['title']; ?> </h1>
<p class="para"><?php echo $row1['desc']; ?></p>
<?php }?>
</body>
</html>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^post/([0-9]+) post.php?pid=$1 [NC]
</IfModule>
When i'm running this code, url shows like this:
http://localhost/blog/post.php?pid=1
I want to display url like this
http://localhost/blog/post/1
Solution is executed successfully, but css styles not working. Css styles placed in css folder

.htaccess is used to parse the url, its not to generate the URL. You need to rewrite line
<li><?php echo $row['title']; ?></li>
to
<li><?php echo $row['title']; ?></li>
and your .htaccess will work

try this in htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
and you can use a bootsrap class
class bootstrap {
function __construct() {
$url=isset($_GET['url']) ? $_GET['url'] : null;
$url=rtrim($url,'/');
$url = explode('/',$url );
//print_r($url);
if(empty($url[0])) {
require 'controllers/index.php';
$controller = new index();
return false;
}
echo $url .'<br>';
$file = 'controllers/' .$url[0]. '.php';
if(file_exists($file)) {
require $file;
}
else {
require 'controllers/error.php';
$controller = new error();
return false;
}
$controller = new $url[0];
if(isset($url[2])) {
$controller ->{$url[1]}($url[2]);
}
else {
if(isset($url[1])) {
$controller ->{$url[1]}();
}
}
}
}

Try using -
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ post.php?pid=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ post.php?pid=$1

Related

Listing okay, but all other functions are not working

I'm totally new to Codeigniter and Grocery CRUD, but I very like the concept, so I'm trying to make my little app with these frameworks.
So, right now the login system is working and after login on the "home" page, I see my Grocery CRUD table, it's filled, so far it's okay...
My problem is, that I can't use any function: add, edit, view, search is not working, it gives me 404 error for example this is one edit link:
http://subdomain.mysite.com/index.php/home/edit/1
The site is on a subdomain, I don't know if could be relevant or not.
This is my base_url:
$config['base_url'] = '/';
If I change this to '', the base_url(); method doesn't gives me the right value, I assume it's because the subdomain.
This is my complete "Home" view:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My system</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1 class="page-header text-center">My system</h1>
<div class="row">
<div class="container text-right">
<?php
$user = $this->session->userdata('user');
extract($user);
?>
<p class="d-inline"><b>Logged in as:</b> <i><?php echo $fname; ?></i></p>
</div>
<div class="container">
<?php
$crud = new grocery_CRUD();
$this->grocery_crud->set_table('mobil_table');
$this->grocery_crud->columns('mobile_number', 'package_name', 'package_date');
$output = $this->grocery_crud->render();
$this->load->view('mobil.php', $output); ?>
Logout
</div>
</div>
</div>
</body>
</html>
My routes:
$route['default_controller'] = 'user';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['home'] = 'user/home';
What should I correct to make these functions work on this page? Thx :)
You must access your edit page via your controller/method manner
if your controller is user and it has a function edit with a param name $id, your accessing url will be:
http://subdomain.mysite.com/index.php/user/edit/1
or define new route for it:
$route['home/edit'] = 'user/edit';
or more clearly as
$route['home/edit/(:num)'] = 'user/edit/$1';
But you can get clean urls by following these steps:
First create a .htaccess file in your websites root folder with this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
After that change:
$config['base_url'] = 'http://subdomain.mysite.com/';
and
$config['index_page'] = '';
After doing the above steps, you can access your urls without index.php in urls
for example:
http://subdomain.mysite.com/index.php/user/edit/1
will become
http://subdomain.mysite.com/user/edit/1

Create a SEO friendly URL

index.php
-------------
<?php
$connect = mysqli_connect("localhost","root","1234","mspl_test");
$data = mysqli_query($connect,"SELECT * FROM products");
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="POST">
<div class="container">
<table border="1">
<th>ID</th>
<th>Name</th>
<?php
while($row =mysqli_fetch_array($data))
{
echo "<tr><td><a href='exp.php?id=$row[0]&name=$row[1]'>$row[0]</a>
<td><a href='exp.php?id=$row[0]&name=$row[1]'>$row[1]</a>";
}
?>
</table>
</div>
</form>
</body>
</html>
exp.php
-------
<?php
$id = $_GET['id'];
$name = $_GET['name'];
echo "<h1 align='center'>Hello! You've selected $name having id of $id</h1>";
?>
.htaccess
---------
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^product?$ exp.php?id=$1&name=&1
</IfModule>
When user clicks on a link some id or name..it shows the url http://localhost/slug/exp.php?id=1&name=Google Nexus 4. i want to remove this id=1&name=Google Nexus 4 and make it seo friendly. i know i can sanitize my url with .htaccess.but it doesn't work. when i click it still shows the query string urls rather than just product.

mod_rewrite to give PHP get parameters doesn't work

Why is this mod_rewrite not working?
RewriteEngine On
RewriteRule ^([a-zA-Z0-9/_-]+)(|)$ /index.php?url=$1 [L]
RewriteRule ^news/(.*)$ index.php?url=news&id=$1 [NC]
Here's the PHP code for handling the loading of the news:
<?php
$sql = DB::Query("SELECT id,title,longstory FROM news WHERE id = ".filter($_GET['id'])."");
if(DB::NumRows($sql) == 1)
{
while($news = $sql->fetch_assoc())
{
echo '
<div class="box">
<div class="title">
'.$news["title"].'
</div>
<div class="mainBox newsBox" style="float;left">
<div class="boxHeader"></div>
'.html_entity_decode($news['longstory']).'
</div>
</div>';
}
} else
{
?>
<div class='box'>
<div class='title red'>Artikel is niet gevonden.</div>
<div class='mainBox'>
Jammer genoeg is dit nieuws artikel niet gevonden!
</div>
</div>
<?php
}
?>
If I use http://127.0.0.1/index.php?url=news&id=48 it's working, but http://127.0.0.1/news/48 doesn't, even though I have added the mod_rewrite rule in my .htaccess.
your first rule does match the /news/48 pattern as well, change the order of the rules and put the specific one ^news/(.*)$ first

codeigniter form not working

I am trying to create a search form in my codeigniter site header, however everytime the form is submitted, I receive a 404 error saying the page cannot be found! I have attempted to create a link to a test page and this gave me the same error.
Please observe my code below.
view(site_header)
<?php echo doctype(); ?>
<html lang="en">
<link href="<?php echo base_url(); ?>styles/style.css" type="text/css" rel="stylesheet"/>
<head>
<title>/title>
<div id="container">
<div id="search">
<?php
echo form_open('search_keyword');
echo form_label("Stumble a search ", "searchfor");
echo form_input("search","search");
echo form_submit("getSearch","Search");
echo form_close(); ?>
</div>
</div>
</head>
</html>
model (model_search)
<?php
class Model_search extends CI_Model {
public function get_results($search_term){
$query = $this->db->query('SELECT embed, title FROM videos WHERE tags LIKE '%$search_term%' order by RAND() LIMIT 1');
return $query->result();
}
}
?>
Controller (site.php) default controller
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index(){
$this->home();
}
public function home(){
$this->load->model("model_get");
$data["results"] = $this->model_get->getRand();
$this->load->view("site_header");
$this->load->view("site_content", $data);
$this->load->view("site_footer");
}
public function search_keyword()
{
$this->load->model('model_search');
$search_term = $this->input->post('search');
$data['results'] = $this->model_search->get_results($search_term);
$this->load->view('site_header');
$this->load->view('search_content',$data);
$this->load->view('site_footer');
}
}
?>
Results page (search_content)
<body>
<link href="<?php echo base_url(); ?>styles/style.css" type="text/css" rel="stylesheet"/>
<div id="container">
<div id="intro">
<?php echo heading("Search Results",1);?>
</div>
<div id ="content">
<p>Stumble videos related to <?php echo $search_term; ?> </p>
<?php
foreach ($results as $row) {
$title = $row->title;
$vid = $row->embed;
}
echo heading($title, 3);
echo $vid;
?>
</div>
</div>
</body>
perhaps I am missing something obvious, however I think it may be to do with my .htaccess file which is posted below
(.htaccess)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /code/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
CodeIgniter URLs need both the controller name and the method.
form_open('site/search_keyword')
Maybe a stupid question, but did you remove the "index.php" from the $config['index_page'] variable in your config.php ?
And if you don't use any route to link 'search_keyword' to 'site/search_keyword', be sure to use the form_open('site/search_keyword') as specified previously.
Btw, here's the .htaccess I always use on my Codeigniter projects to rewrite the urls. Just add your RewriteBase on this to make it work.
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|img|assets|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

Changing Page URL To PHP

Hi I want to convert pages URL like www.example.com/pictures.php?title=yyoy to something like www.example.com/page/yoyo.php
How can I do this?
www.example.com/pictures.php?title=yyoy these pages are generated when someone upload image to my website so I want to turn these types of URL to like above shown example.
Because these pages can't be index in search engines. Thanks
Picture.php
<?php
include("connection.php");
if(isset($_GET['title'])){
$page_id = $_GET['title'];
$select_query = "select * from save_data where Title='$page_id'";
$run_query = mysql_query($select_query);
while($row=mysql_fetch_array($run_query)){
$post_id = $row['ID'];
$post_title = $row['Title'];
$post_image = $row['Name'];
$page_title = $_GET['title'];
header('uploads/ ' . $page_title . '.php')
?>
<center>
<h2>
<a href="pictures.php?title=<?php echo $post_title; ?>">
<?php echo $post_title; ?>
</a></center>
</h2>
<center><img src="uploads/<?php echo $post_image; ?>" /></center>
<?php } }?>
I have not tested this, but you should use rules similar to the following in your .htaccess file:
RewriteEngine on
RewriteRule ^pictures.php?title=(.+)$ page/$1.php [R]
Note that, you will need mod_rewrite enabled for this to work in apache2
There's a good answer here: Masking $_GET variables of the URL when passing pages in PHP
That will tell you how to mask your URLs.
Alternatively, you could have these files uploaded to a directory in your file structure called "page/" Then you can access the files (as a download) inside the page directory with www.example.com/page/filename.php
Usually I would create an in-between page when posting from a form, but in your case your link looks like this:
www.example.com/pictures.php?title=yoyo
So it directs you to pictures.php with a variable 'title' in the URL.
So on your pictures.php page use:
$page_title = $_GET['title'];
And when you want to go to yoyo.php:
header('Location: $page_title.php')
From your edit this code will redirect the page but it will not display the last HTML:
<?php
include("connection.php");
if(isset($_GET['title'])){
$page_id = $_GET['title'];
$select_query = "select * from save_data where Title='$page_id'";
$run_query = mysqli_query($select_query);
while($row=mysqli_fetch_assoc($run_query)){
$post_id = $row['ID']; //Just make sure the values between [''] match -
$post_title = $row['Title']; //your columns in database.
$post_image = $row['Name'];
}
header('Location:uploads/$post_title.php');
?>
This will be left out:
<center>
<h2>
<a href="pictures.php?title=<?php echo $post_title; ?>">
<?php echo $post_title; ?>
</a></center>
</h2>
<center><img src="uploads/<?php echo $post_image; ?>" /></center>
<?php } }?>
URE This one
RewriteEngine on
RewriteRule ^pictures.php?title=(.+)$ page/$1.php [R, L]
You should use the .htaccess file. Add this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^picture/([a-zA-Z0-9]+).php$ pictures.php?title=$1
</IfModule>
If you're using a local server like WAMP, be sure to turn on de mod_rewrite or rewrite_module of apache and reboot the server.
You should also Google for Regular Expressions.
Let me know if it was useful.

Categories