modify url via htaccess passing and capturing two parameters - .htaccess

I need to pass two parameters via url when accessing the archive.php page.
The URL is the same as sitename.com/archive.php?page="2"&category="car".
I would like htaccess to become like this
sitename.com/car/2/
Next I would like to capture the two parameters.
Can anyone explain to me how to edit the htaccess file and how to capture the two parameters from the URL.
My current htaccess is as follows:
RewriteEngine ON
Options -Indexes -MultiViews
DirectoryIndex home.php
RewriteRule ^sitemap.xml ./sitemap.php [L,QSA]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ core.php [L]
page core.php:
<?php include './include/include.php'; ?>
<?php require './config.php'; ?>
<?php
$url = basename(strtolower($_SERVER['REQUEST_URI']));
$cleanUrl = filter_var($url, FILTER_SANITIZE_STRING);
$st2 = $PDO->query("SELECT * FROM sb_article WHERE url = '$cleanUrl'");
while($recordArt = $st2->fetch()){
$artUrl = $recordArt['url'];
}
if($artUrl !== $cleanUrl){
$st = $PDO->query("SELECT * FROM sb_category WHERE categoria = '$cleanUrl'");
while($recordCat = $st->fetch()){
$catTit = $recordCat['categoria'];
}
}
switch($cleanUrl){
case $artUrl:
single();
break;
case $catTit:
echo "sono una categoria";
break;
case "contatti":
echo "sono una la pagina contatti";
break;
default:
echo "articolo non trovato";
break;
}
function single(){
$urlArticle = basename(strtolower($_SERVER['REQUEST_URI']));
require './include/include.php';
require './classes/class.single.php';
$single= new showArticle($urlArticle, $PDO);
$showSingle = $single->single();
if($showSingle !== NULL){ foreach($showSingle as $list){
$id = $list['id'];
$titolo = $list['titolo'];
$testo = html_entity_decode($list['testo']);
$descrizione = $list['descrizione'];
$categoria = $list['categoria'];
$url = $list['url'];
$img = $list['img'];
$data = $list['data'];}}
require( TEMPLATE_PATH . "/single.php");
}
?>

I was unable to solve the problem. The url is not changed. I try to explain myself better.
I have this address
localhost/simpleblog/archive.php?Page=2&category=car
i want to convert it to
localhost/simpleblog/archive/2/car
my current htaccess is this:
RewriteEngine ON
Options -Indexes -MultiViews
DirectoryIndex home.php
RewriteRule ^sitemap.xml ./sitemap.php [L,QSA]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ core.php [L]

Related

403 Forbidden Access While Calling Login Function CodeIgniter 3x

I code an application on ubuntu. The application runs well without any problem. But, this problem starts when I decided to run the CI based application locally using XAMPP at Windows.
Front end works without a problem. But, when I tried to login, it gave me "Error 403 Access Forbidden! You don't have permission to access the requested object. It is either read-protected or not readable by server". It seems the application can't call the loginAction function from "System" controller while in my ubuntu client, it worked without 403 forbidden error.
http://localhost/myProject/System/loginAction
I've tried this thread but it doesn't come with a solution to my case.
CodeIgniter error 403
I've set the project folder's permission to 755 in before (But it doesn't affect the permission really at windows right?)
I also did something to config.php file at these lines :
$config['base_url'] = 'http://localhost/myProject/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
My code :
Login view (login.php) :
<form action="<?= base_url(); ?>System/loginAction" method="POST">
<input type="username" name="username" class="form-control" placeholder="Username" required maxlength="50" autocomplete="off">
<input type="password" name="password" class="form-control" placeholder="Password" required maxlength="30">
<button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
</form>
Controller (System.php) :
function loginAction() {
$username = $this->input->post('username');
$password = $this->input->post('password');
$where = array(
'username' => $username,
'password' => md5($password),
);
$cekLogin = $this->Admin_Model->cekLogin('mytb', $where)->num_rows();
if($cekLogin > 0 && $cekLogin < 2) {
$dataUser = $this->Admin_Model->getUserData('mytb', $where);
$dataSession = array(
//some session data here
);
if($dataUser[0]['activeStatus'] == '1') {
$this->Admin_Model->updateLastLogin($dataUser[0]['idUser']);
$this->session->set_userdata($dataSession);
redirect(base_url().'Admin/');
} else {
$this->session->set_flashdata('alert_warning', 'This username is in inactive status!');
redirect(base_url().'Login/');
}
} else {
$this->session->set_flashdata('alert_gagal', 'Wrong username or password!');
redirect(base_url().'Login/');
}
}
.htaccess :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
*Note I haven't touched the .htaccess that is located in the "application" folder.
I'm expecting that the application can do login process through localhost at windows client perfectly as same as localhost at ubuntu client does. I've been stuck at the same problem for hours ;-;
The problem is with your controller name. When you browse your url it is pointing to the codeigniter's system folder. You may need to change your controller name to fix it.

HTAccess Rewrite Rule URL from Meta Title

How can I rewrite a rule for the url
navi.php?a=815&lang=eng
and a meta title
How can I post a Video
into
/en/How-can-I-post-a-Video
Can the Meta Title be fetched somehow to add it to url?
Instead of passing the params in the url, you need to form a url with meta tag title. From the url take the arguements and match with the database and get the result data. This is how the .htaccess will work.
You can make a rewrite rule like so:
RewriteRule ^(.*)$ index.php?params=$1 [NC]
This will make your actual php file like so:
index.php?params=value&param=value
And your actual URL would be like so:
http://url.com/params/param/value/param/value
And in your PHP file you could access your params by exploding this like so:
<?php
$params = explode( "/", $_GET['params'] );
for($i = 0; $i < count($params); $i+=2) {
echo $params[$i] ." has value: ". $params[$i+1] ."<br />";
}
?>

Redirect a specific url but only on mobile or certain screen sizes

Ok so here is the issue. I use a cms to run a ecommerce website. It uses a mobile site addon when the user is on a phone or ipad. I want to redirect a specific url, but only when in mobile. I would like to keep the url the same for desktop.
example:
Redirect /desktop-categories/ site.com/mobile-categories
How do I do this and specify to only redirect when user is on mobile?
First you'll need to determine if their browser is a web browser on a mobile device or not. Because mobile phones typically have a small screen width, you can redirect visitors to your mobile site if they have a screen width of less than or equal to 800 pixels.
Javascript window.location Method 1
<script type="text/javascript">
if (screen.width <= 800) {
window.location = "http://m.domain.com";
}
</script>
You can use a .htaccess redirect to transfer visitors based upon the MIME types the browser supports. For example, if the user's browser accepts mime types that include WML (Wireless Markup Language), then most likely it is a mobile device.
.htaccess URL rewrite redirects 1
RewriteEngine On
# Check for mime types commonly accepted by mobile devices
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^ http://m.domain.com%{REQUEST_URI} [R,L]
Redirecting Mobile Users by Screen Size Instead of Device Type 2
var Responsive = {
maxPhoneWidth: 775,
maxTabletWidth: 925,
//REDIRECT BASED ON CONFIGURABLE MAX WIDTH THRESHOLD
maxWidthRedirect: function (maxWidth, url) {
var viewport = this.getWindowSize();
//ADD EXCLUSION IN HASH IN CASE DESKTOP VIEWING IS INTENDED
if (window.location.hash != '#desktop' && viewport.width < maxWidth)
window.location.href = url;
},
//REDIRECT BASED ON RECOMMENDED PHONE WIDTH THRESHOLD
mobileRedirect: function (url) {
this.maxWidthRedirect(this.maxPhoneWidth, url);
},
//REDIRECT BASED ON RECOMMENDED TABLET WIDTH THRESHOLD
tabletRedirect: function (url) {
this.maxWidthRedirect(this.maxTabletWidth, url);
},
//DETERMINE CROSS-BROWSER SCREEN SIZE
getWindowSize: function () {
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight || e.clientHeight || g.clientHeight;
return { width: x, height: y };
}
};
You can see the code in action at the following link and trying different browser sizes.
References
How to redirect your website to its mobile version
Redirecting Mobile Users by Screen Size Instead of Device Type

Flowplayer Secure Streaming "Stream not found"

My client wants his videos hidden and not having the possibility to be downloaded or copied (or at least to make it difficult to do).
I'm trying to use flowplayer secure streaming, but I cannot make it work!
I'm getting this error:
200, Stream not found, NetStream.Play.StreamNotFound, clip: '[Clip]
'secure/ad722768cfa6f10b51b7e317c8dd1ca4/1417957647/v.mp4"
It says the video was not found, but it's placed on secure/v.mp4 as it should (right?)
UPDATE1
I forgot to mention that I have the required apache rewrite rule inside secure folder…
secure/.htaccess
RewriteEngine on
RewriteBase /secure
RewriteRule ^(.*)/(.*)/(.*)$ video.php?h=$1&t=$2&v=$3
RewriteRule ^$ - [F]
RewriteRule ^[^/]+\.(flv|mp4)$ - [F]
UPDATE2
I did it! It was a dumb simple thing:
I was testing with easyphp webserver, and the url was localhost/v/index.html
I did a test moving all the content from /v folder to the root and it worked!
And now I learned in htaccess I need to put the full path on RewriteBase starting from the root, in my case I needed to set this:
RewriteBase /v/secure
The codes:
index.html
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="flowplayer-3.2.13.min.js"></script>
<script>
$(document).ready(function() {
$f("player", "flowplayer-3.2.18.swf", {
plugins: {
secure: {
url: "flowplayer.securestreaming-3.2.9.swf",
timestampUrl: "sectimestamp.php"
}
},
clip: {
baseUrl: "secure",
url: "v.mp4",
urlResolvers: "secure",
scaling: "fit",
}
});
});
</script>
</head>
<body>
<div id="player"></div>
</body>
sectimestamp.php
<?php
echo time();
?>
secure/video.php
<?php
$hash = $_GET['h'];
$streamname = $_GET['v'];
$timestamp = $_GET['t'];
$current = time();
$token = 'sn983pjcnhupclavsnda';
$checkhash = md5($token . '/' . $streamname . $timestamp);
if (($current - $timestamp) <= 2 && ($checkhash == $hash)) {
$fsize = filesize($streamname);
header('Content-Disposition: attachment; filename="' . $streamname . '"');
if (strrchr($streamname, '.') == '.mp4') {
header('Content-Type: video/mp4');
} else {
header('Content-Type: video/x-flv');
}
header('Content-Length: ' . $fsize);
session_cache_limiter('nocache');
header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
$file = fopen($streamname, 'rb');
print(fread($file, $fsize));
fclose($file);
exit;
} else {
header('Location: /secure');
}
?>
I already tried this > Flowplayer Secure Streaming with Apache but I also get the error above.
Does anyone use flowplayer secure streaming? What am I doing wrong?
I figured out what was wrong, in htaccess you need to put the full path on RewriteBase starting from the root, in my case I needed to set this:
RewriteBase /v/secure

Opencart SEO URL for product filters

I recently got a chance to work on Opencart and I got a custom-made theme (Pav foodstore).
I am trying to implement auto-seo URLs in my project. For doing so, I downloaded a very good extension from Navneet Singh for auto-seo URLs. The problem is, it doesn't come with SEO URLs for product filters. Tried a few modifications, but I can't seem to get it working right.
Following is the VQmod xml file code :
<?xml version="1.0" encoding="UTF-8"?>
<modification>
<id>CUSTOM CHANGES DONE BY NAVNEET SINGH</id>
<version>1</version>
<vqmver>1.0.0</vqmver>
<author>NAVNEET SINGH</author>
<!-- SEO URLS -->
<file error="abort" name="system/library/response.php">
<operation>
<search position="after"><![CDATA[
public function output() {
]]></search>
<add><![CDATA[
if ($this->output) {
// REPLACE URLS WITH SEO URLS
function my_repl($value) {
if( defined(ENT_COMPAT) && defined(ENT_HTML401) && defined(ENT_IGNORE) ) {
$val=str_replace(" ","-",strtolower(trim(strtr(html_entity_decode(strip_tags($value),ENT_COMPAT|ENT_HTML401|ENT_IGNORE,"UTF-8"),"`~!##$%^&*()-_=+\\|[{]};:'\",<.>/?"," "))));
return rawurlencode($val); }
elseif( defined(ENT_COMPAT) && defined(ENT_IGNORE) ) {
$val=str_replace(" ","-",strtolower(trim(strtr(html_entity_decode(strip_tags($value),ENT_COMPAT|ENT_IGNORE,"UTF-8"),"`~!##$%^&*()-_=+\\|[{]};:'\",<.>/?"," "))));
return rawurlencode($val); }
else {
$val=str_replace(" ","-",strtolower(trim(strtr(html_entity_decode(strip_tags($value),ENT_COMPAT,"UTF-8"),"`~!##$%^&*()-_=+\\|[{]};:'\",<.>/?"," "))));
return rawurlencode($val); }
}
function my_preg_replace($m1) {
if($m1[1]=='information/information') $p2 = 'i';
elseif($m1[1]=='product/category') $p2 = 'c';
elseif($m1[1]=='product/category' && $m1[2]=='filter') $p2 = 'f';
elseif($m1[1]=='product/manufacturer/info') $p2 = 'm';
elseif($m1[1]=='product/product' && $m1[2]=='product_id') $p2 = 'pp';
elseif($m1[1]=='product/product' && $m1[2]=='manufacturer_id') $p2 = 'pm';
elseif($m1[1]=='product/product' && $m1[2]=='path') $p2 = 'pc';
else return;
// IF IMG TAG, USE IMG ALT TEXT IN SEO URL; ELSE, USE ANCHOR TEXT IN SEO URL
if($p2=='i' || $p2=='c' || $p2=='m' || $p2=='pp') {
if( preg_match( '#<img.*alt="([^"]*)"#iUs', $m1[4], $m2 ) )
$u1 = my_repl($m2[1]);
else
$u1 = my_repl($m1[4]);
return $p2 . trim($m1[3]) . "-" . $u1 . '">' . $m1[4] . "</a>";
}
elseif($p2=='pc') {
if( preg_match( '#<img.*alt="([^"]*)"#iUs', $m1[5], $m2 ) )
$u1 = my_repl($m2[1]);
else
$u1 = my_repl($m1[5]);
return $p2 . trim($m1[4]) . "-c" . trim($m1[3]) . "-" . $u1 . '">' . $m1[5] . "</a>";
}
elseif($p2=='pm') {
if( preg_match( '#<img.*alt="([^"]*)"#iUs', $m1[5], $m2 ) )
$u1 = my_repl($m2[1]);
else
$u1 = my_repl($m1[5]);
return $p2 . trim($m1[4]) . "-m" . trim($m1[3]) . "-" . $u1 . '">' . $m1[5] . "</a>";
}
}
// PATTERNS TO SEARCH FOR SEO URL REPLACEMENT
$p1s = array(
"#index\.php\?route\=(product/category)&(path)=([0-9_]+?)\">(.*)</a>#iUs",
"#index\.php\?route\=(product/category)&(path)=([0-9_]+?)&filter=([0-9]+?)\">(.*)</a>#iUs",
"#index\.php\?route\=(product/manufacturer/info)&(manufacturer_id)=([0-9]+?)\">(.*)</a>#iUs",
"#index\.php\?route\=(information/information)&(information_id)=([0-9]+?)\">(.*)</a>#iUs",
"#index\.php\?route\=(product/product)&(product_id)=([0-9]+?)[^\"]*\">(.*)</a>#iUs",
"#index\.php\?route\=(product/product)&(path)=([0-9_]+?)&product_id=([0-9]+?)\">(.*)</a>#iUs",
"#index\.php\?route\=(product/product)&(manufacturer_id)=([0-9]+?)&product_id=([0-9]+?)\">(.*)</a>#iUs",
);
$this->output = preg_replace_callback( $p1s, "my_preg_replace", $this->output );
}
]]></add>
</operation>
</file>
And this is my .htaccess code block :
RewriteEngine On
RewriteRule ^i([0-9]+)-(.*)$ index.php?route=information/information&information_id=$1 [L]
RewriteRule ^c([0-9_]+)-(.*)$ index.php?route=product/category&path=$1 [L]
RewriteRule ^c([0-9_]+)-f([0-9_]+)-(.*)$ index.php?route=product/category&path=$2&filter=$1 [L]
RewriteRule ^m([0-9]+)-(.*)$ index.php?route=product/manufacturer/info&manufacturer_id=$1 [L]
RewriteRule ^pp([0-9]+)-(.*)$ index.php?route=product/product&product_id=$1 [L]
RewriteRule ^pm([0-9]+)-m([0-9]+)-(.*)$ index.php?route=product/product&manufacturer_id=$2&product_id=$1 [L]
RewriteRule ^pc([0-9]+)-c([0-9_]+)-(.*)$ index.php?route=product/product&path=$2&product_id=$1 [L]
Any help would be appreciated.

Resources