I keep getting a 500 internal server error when trying to rewrite these one specific URLs/files (item.php & purchase.php), the rest works.
I have tried many ways to fix this but nothing seemed to work, which is weird because all other URLs do work, but these 2 just seem to not want to work for some reason.
.htaccess file
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
ErrorDocument 500 "500"
ErrorDocument 404 "404"
ErrorDocument 403 "403"
RewriteRule ^users/([^/]*)$ /user/profile.php?username=$1
RewriteRule ^users/([^/]*)/inventory$ /user/GetUserInventory.php?username=$1
RewriteRule ^market/item/([^/]*)$ /market/item.php?id=$1
RewriteRule ^market/item/([^/]*)/purchase$ /market/purchase.php?id=$1
RewriteRule ^community/([^/]*)$ /communities/view.php?id=$1
RewriteRule ^community/([^/]*)/join$ /communities/join.php?id=$1
RewriteRule ^community/([^/]*)/manage$ /communities/manage.php?id=$1
RewriteRule ^game/([^/]*)$ /games/view.php?id=$1
item.php
<?php
include_once('../private/header.php');
$item = $handler->query("SELECT * FROM items WHERE id=" . $_GET['id']);
$gB = $item->fetch(PDO::FETCH_OBJ);
echo '
<div class="col s12 m9 l8">
<div class="container" style="width:100%;">
<div class="content-box" style="border-radius:0;">
<div class="left-align">
</div>
<div class="row">
<div class="col s12 m6 l3 center-align">
<img src="'.$cdnServer.'/items/thumbnails/'.$gB->image.'.png" class="responsive-img">
</div>
<div class="col s12 m6 l6">
<div style="padding-left:25px;overflow:hidden;">
<div style="font-size:26px;font-weight:300;">'.$gB->name.'
<b style="text-transform:uppercase;font-size:12px;">'.$gB->type.'</b>
</div>
<div style="color:#777;font-size:14px;">'.$gB->description.'</div>
</div>
</div>
<div class="col s12 m3 l3 center-align" style="padding-top:15px;">
<center>
';
if ($gB->onsale == 1){
echo 'Purchase';
} else {
echo '<a class="modal-trigger waves-effect waves-light btn grey darken-2">Offsale</a>';
}
echo '
</center>
<div style="height:15px;"></div>
<center><b style="text-transform:uppercase">Creator</b></center>
<center>'.$gB->creator.'</center>
';
if($gB->collectable == 'true'){
if($gB->amount == 0)
echo '
<center><span style="color:red">Sold Out</span></center>
';
}else{
echo '
<center><span style="color:red">'.$gB->amount.' Remaining</span></center>
';
}
echo '
<div style="height:25px;"></div>
</div>
</div>
<div style="padding-top:25px;">
<div class="row" style="margin-bottom:0;">
<div class="col s12 m12 l3 center-align">
<div style="font-size:20px;">'.$gB->created.'</div>
<div style="color:#999;font-size:14px;">Time Created</div>
</div>
<div class="col s12 m12 l3 center-align">
<div style="font-size:20px;">'.$gB->created.'</div>
<div style="color:#999;font-size:14px;">Last Updated</div>
</div>
<div class="col s12 m12 l3 center-align">
<div style="font-size:20px;">???</div>
<div style="color:#999;font-size:14px;">Owners</div>
</div>
</div>
</div>
</div>
';
include_once('../private/footer.php');
purchase.php
<?php
include_once('../private/config.php');
if ($user){
$money=$myu->CurrencyCoins;
$id=$_GET['id'];
$item = $handler->query("SELECT * FROM items WHERE id=" . $id);
$gB = $item->fetch(PDO::FETCH_OBJ);
$amount=$gB->amount;
if ($gB->onsale == 1){
if ($money >= $gB->price){
if ($gB->collectable != "true"){
if ($amount != 0){
$new = ($money - $gB->price);
$handler->query("UPDATE `users` SET `CurrencyCoins`='".$new."' WHERE `id`='".$myu->id."'");
$handler->query("INSERT INTO inventory (item,user) VALUES (".$id.",".$myu->id.")");
}
} else {
if ($amount >= 1){
$amount1=($amount - 1);
$new = ($money - $gB->price);
$handler->query("UPDATE `users` SET `CurrencyCoins`='".$new."' WHERE `id`='".$myu->id."'");
$handler->query("UPDATE `items` SET `amount`='".$amount1."' WHERE `id`='".$gB->id."'");
$handler->query("INSERT INTO inventory (item,user) VALUES (".$id.",".$myu->id.")");
} else {
echo '<center><h2>Item is sold out!</h2></center>';
}
}
}
} else {
echo '<center><h2>Item not on sale!</h2></center>';
}
}
echo '<head><meta http-equiv="refresh" content="1; url='.$serverUrl.'/account/character"></head>';
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
These directives should be at the end of your .htaccess file, after the other rewrites. They are also incorrect - although by placing them at the end of the file it will avoid the immediate issue, but could still cause problems with other URLs.
In its current state, when you request example.com/market/item/1 (where it would seem, /market is a physical directory, and /item.php is a file in that directory) then...
In brief... it results in an endless rewrite loop:
/market/item/1.php
/market/item/1.php.php
/market/item/1.php.php.php
/market/item/1.php.php.php.php
etc.
Which breaks (after 10 iterations) with a 500 response being sent to the browser.
A very similar process happens when requesting /market/item/1/purchase:
/market/item/1/purchase.php
/market/item/1/purchase.php.php
/market/item/1/purchase.php.php.php
/market/item/1/purchase.php.php.php.php
etc.
In detail...
REQUEST_FILENAME is /market/item (ignoring the directory-prefix) and PATH_INFO is /1 (important for later). /market/item is not a directory (1st condition), but /market/item.php is a file (2nd condition) - so both conditions are successfully met.
The RewriteRule directive then rewrites /market/item/1 to /market/item/1.php (clearly incorrect and not the intention). Since there is no L flag on this rule, processing continues... for the sake of the remaining rules, the PATH_INFO (from the initial request, /1) is appended to the resulting URL to become /market/item/1.php/1 (the DPI flag - discard path info - was created to prevent this specific behaviour).
/market/item/1.php/1 does not match any further rules in the current pass, so the rewrite engine starts over at the top with /market/item/1.php.
REQUEST_FILENAME is again /market/item and PATH_INFO is now /1.php. /market/item is not a directory (1st condition), but /market/item.php is a file (2nd condition) - so both conditions are successfully met a second time.
The RewriteRule directive then rewrites /market/item/1.php to /market/item/1.php.php. Since there is no L flag on this rule, processing continues... for the sake of the remaining rules, the PATH_INFO (from the request, /1.php this time) is appended to the resulting URL to become /market/item/1.php.php/1.php.
/market/item/1.php.php/1.php does not match any further rules in the current pass, so the rewrite engine starts over at the top with /market/item/1.php.php.
GOTO #4 (with updated URL-path) etc. etc. etc. rewrite loop, 500 error.
And a very similar process happens when requesting /market/item/1/purchase. The REQUEST_FILENAME is the same /market/item (so it again checks that /market/item.php exists, not purchase.php), except that the PATH_INFO is /1/purchase (not /1). And the initial URL-path that the .php extension is appended to is naturaly /market/item/1/purchase (not /market/item/1).
Fix
If you've followed that "confusing muddle", you'll see that the condition that checks for the existence of the "path/to/file" + ".php" is not necessarily the same as the rule that actually rewrites the request to "URL-path" + ".php". ("path/to/file" is not the same as the "URL-path"). To fix this, it should be written as:
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]
No real need for the directory check here, since if it was a directory, the file check that follows must fail (unless you have directory names that end in .php). The %{DOCUMENT_ROOT}/$1.php check is now effectively "the same" as $1.php (the file being rewritten to).
The literal dot does not need to be backslash escaped in the RewriteCond TestString - this is an "ordinary" string, not a regex.
Don't forget the L flag(s). And this rule block should now go at the end of the .htaccess file.
Summary
Options -Indexes
RewriteEngine on
ErrorDocument 500 "500"
ErrorDocument 404 "404"
ErrorDocument 403 "403"
RewriteRule ^users/([^/]*)$ /user/profile.php?username=$1 [L]
RewriteRule ^users/([^/]*)/inventory$ /user/GetUserInventory.php?username=$1 [L]
RewriteRule ^market/item/([^/]*)$ /market/item.php?id=$1 [L]
RewriteRule ^market/item/([^/]*)/purchase$ /market/purchase.php?id=$1 [L]
RewriteRule ^community/([^/]*)$ /communities/view.php?id=$1 [L]
RewriteRule ^community/([^/]*)/join$ /communities/join.php?id=$1 [L]
RewriteRule ^community/([^/]*)/manage$ /communities/manage.php?id=$1 [L]
RewriteRule ^game/([^/]*)$ /games/view.php?id=$1 [L]
# Append .php on remaining requests
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]
Related
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
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
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
How can I set an iframe src as the location in the $_GET properly.
Currently i'm trying this:
.htaccess
RewriteRule ^stream/(.*)$ index.php?p=stream&link=$1
stream.php/http://bbc.co.uk
<?php
echo '
<div class="container" style="height:1000px;">
<iframe src="'.$_GET['link'].'" frameborder="0" width="100%" height="100%"></iframe>
</div>
';
?>
This sets the iframe to go to 127.0.0.1/stream/http://bbc.co.uk when it should just go to bbc.co.uk
As I indicated in my comment, you need to rewrite php files to remove the .php so that the rule you posted matches, e.g.:
RewriteRule (\w+).php $1
RewriteRule ^stream/(.*)$ index.php?p=stream&link=$1
Which means your link will be re-written as so:
http://example.com/stream.php/http://bbc.co.uk
http://example.com/stream/http://bbc.co.uk
http://example.com/index.php?p=stream&link=http://bbc.co.uk
Then your php will output:
<div class="container" style="height:1000px;">
<iframe src="http://bbc.co.uk" frameborder="0" width="100%" height="100%"></iframe>
</div>
Which works as expected.
The url itself is not correct ie. it should have been in the form
stream.php?link=http://bbc.co.uk
Then you will get it in:
$_GET['link']
Other way would be store a mapping of the above get variable with the corresponding url:
ie.
var urlsMapping = {"bbc":"http://bbc.co.uk","toi":"http://timesofindia.indiatimes.com"}
Then make your url like:
stream.php?link=bbc
Set iframe src as
urlsMapping[<?=$_GET['link']?>]
Simply try this
RewriteRule ^stream/(.+)$ index.php?p=stream&link=$1 [L,QSA]
I hope this will hep you.
I fixed this using preg_replace
Probably not the best option, however it did work.
<?php
$link = preg_replace('~http:/~si', '', $_GET['link']);
echo '
<div class="container" style="height:1000px;">
<iframe src="http://'.$link.'" frameborder="0" width="100%" height="100%"></iframe>
</div>
';
?>
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]