Rewrite Query string in URL using .htaccess - .htaccess

How to rewrite my website's url from "website.com/example.php?site=youtube" to "website.com/example/youtube" where site is a variable?
Should i change the url in the php file too?

You can use this in your .htaccess file:
RewriteEngine On
RewriteRule ^example/([^/]*)$ /example.php?site=$1 [L]
That will leave you with the following URL: http://website.com/example/youtube. Make sure you clear your cache before you test this.

$connection = mysqli_connect("localhost" , "root" , "" , "test");
$query = "SELECT * FROM websites";
$select_all_sites = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($select_all_sites)) {
$site_title = $row['site_title'];
echo "<a class='item' href='site.php?website=$site_title'> $site_title</a>";
On the other page which is called site.php i am using this code to get the variable from the sites.php page
function myname(){
if(isset($_GET['website'])){
$variable = $_GET['website'];
echo $variable;
}
}

Related

.htaccess with url shortener

I've made a url shortener app , but can't seem to work my way around .htaccess to indeed make the urls shorter.
(I'm using xampp)
my domain is http://localhost/smd
i would like the user to be able to add a 7 digit code after smd http://localhost/smd/code
and redirect them to their page
my current .htaccess code is like that:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^[a-zA-Z0-9_{7}]*$ redirect.php?shortened=$1 [L]
which redirects the user to to a another page in my smd directory instead of the correct one.
Thanks in advance!
my redirect file:
<?php
include_once './class/Short.php';
date_default_timezone_set('Europe/Athens');
if(isset($_GET['shortened'])) {
$short = new Short();
$code = $_GET['shortened'];
$short->shortened = $code;
$short->getOneUrlByShort();
$expire = $short->getExpiryFromShortened($code);
$comp = (date('Y-m-d H:i:s')<=$expire['expiry_date']);
$enabled = $short->is_enabled;
if ($url = $short->getUrl($code) And $comp And $enabled == 1) {
$redirect = $url['original'];
header("Refresh: 3;".$redirect);
die();
}else if($url = $short->getUrl($code) And !$comp And $short->renewable == 1 And $enabled == 1){
session_start();
$short->renewable = 0;
$newRenewVal = $short->renewable;
$newExpiration = strtotime('+'.$short->active_period.' minutes');
$formattedExpi = date('Y-m-d H:i:s', $newExpiration);
$original = $url['original'];
$_SESSION['original'] = $original;
$_SESSION['expiration_date'] = $formattedExpi;
$short->renewUrl($code, $formattedExpi, $newRenewVal);
header('Location: http://localhost/smd/expiredAndRenew.php');
}else {
header('Location: http://localhost/smd/urlExpired.php');
}
}
?>
My current directory looks like that 1
With your shown samples could you please try following. Please make sure you clear your browser cache before testing your URLs. Also this simply grabs everything coming after smd(with ignorecase emabled) in uri and passes it to your redirect.php as query string. In case you are looking for specific string/match for text after smd then kindly do let us know more clearly on that part.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_]{7})/?$ redirect.php?shortened=$1 [L]

Can htaccess override CodeIgniter's routes.php

I'm wondering if it's possible to override routes.php rules with htaccess in Codeigniter 3.
For example, in order to point dynamic subdomains to the same controllers and pass the subdomain as a parameter, routes.php falls short for doing this, while in htaccess is really simple to do.
Another example is to mask query strings with URL segments. Routes.php doesn't allow to use query strings, but htaccess is, again, perfect for this.
So, as a general question, is it possible to use htaccess for all routing in CodeIgniter instead of using routes.php?
I think you can use htaccess for static routing in codeigniter. But for dynamic routing application base like http://localhost/myproject/user/1
you have to use routes.php.
Codeigniter routes config is used to route module/controller/method/variable patterns.
I think, domain/subdomains go out from this config, however you could use dinamic base_url, based on $_SERVER variables, and then get the string (subdomain) from the specific controller.
From my config, on CI 2.x
$config['base_url'] = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
$config['base_url'] .= '://'. $_SERVER['HTTP_HOST'];
$config['base_url'] .= isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != '80' && $_SERVER['SERVER_PORT'] != '443' ? ( ':'.$_SERVER['SERVER_PORT'] ) : '';
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
then do something like this...
$server = $_SERVER['HTTP_HOST'];
$domain = preg_replace('#^www\.(.+\.)#i', '$1', $server);
$domain = $this->extract_domain($domain);
$subdomain = $this->extract_subdomains($server);
function extract_domain($domain)
{
if(preg_match("/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i", $domain, $matches))
{
return $matches['domain'];
} else {
return $domain;
}
}
function extract_subdomains($domain)
{
$subdomains = $domain;
$domain = $this->extract_domain($subdomains);
$subdomains = rtrim(strstr($subdomains, $domain, true), '.');
return $subdomains;
}

Opencart Search page as SEO url

Trying to get my search results page SEO-friendly .. seems so basic, yet my tags rewrite rule is conflicting it.
(Which also uses the product/search router) -- disabling the tags rewrite, search still doesn't seem to work properly.. it shows whatever it wants, even if I search a nonexistent item. (This behavior doesn't occur under the normal index.php?route=product/search URL)
.htaccess:
RewriteRule ^tags/([^/]*)$ index.php?route=product/search&tag=%{QUERY_STRING} [L]
RewriteRule ^search/([^/]*)$ index.php?route=product/search&search=%{QUERY_STRING} [L]
catalog/controller/startup/seo_url.php:
} elseif ($data['route'] == 'product/search' && $key == 'tag') {
$url .= '/tags/' . str_replace(' ','-',$value);
unset($data[$key]);
} elseif ($data['route'] == 'product/search' && $key !== 'tag') {
$url .= '/search/' . str_replace(' ','-',$value);
unset($data[$key]);
//....
Is there any way to rewrite both of these routes without choosing one or the other?
Using Opencart 2.3.0
catalog/controller/startup/seo_url.php
find:
$this->request->get['route'] = 'error/not_found';
add:
if (strpos($this->request->get['_route_'], 'tag/') !== false) {
$this->request->get['route'] = 'product/search';
$this->request->get['tag'] = str_replace('tag/','',$this->request->get['_route_']);
}
elseif (strpos($this->request->get['_route_'], 'search/') !== false) {
$this->request->get['route'] = 'product/search';
$this->request->get['search'] = str_replace('search/','',$this->request->get['_route_']);
}
else {
$this->request->get['route'] = 'error/not_found';
}
.htaccess:
RewriteRule ^tag/([^/]*)$ index.php?route=product/search&tag=$1 [L]
RewriteRule ^search/(.*)$ index.php?route=product/search&search=$1 [L]

Redirects not working after adding dynamic to static code

We are using for our ecommerce website ECT templates, and installed a code from a programmer that changed the dynamic url to static. However, we have some backlinks that we would like to redirect to the proper page (or we would settle for sending them all to one "all products" page that we have set-up. The redirect we have currently returns a page that says "This product has been removed." Not good!
Here is the code that is on the htaccess file for the dynamic to static:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.*)$ /handle_url.php [L]
Here is the code in the handle_url.php file:
<?php
ob_start();
session_start();
include "vsadmin/db_conn_open.php";
function sli_escape_string($estr){
if(version_compare(phpversion(),'4.3.0')=='-1') return(mysql_escape_string(trim($estr))); else return(mysql_real_escape_string(trim($estr)));
}
$url = $_SERVER['REQUEST_URI'];
$url = trim($url, "/");
$pos = strpos($url,'?');
if ($pos > 0){
$url = substr($url,0,$pos);
}
$pos = strpos($url,".");
if ($pos > 0){
$urlspec = substr($url, 0, $pos);
} else {
}
$store_url = $url;
$sSQL = "SELECT pID FROM products WHERE pURL='" . sli_escape_string($url) . "'";
$query = mysql_query($sSQL);
if ($query && mysql_num_rows($query)>0){
$rs = mysql_fetch_assoc($query);
$_GET['prod']=$rs['pID'];
$explicitid=$rs['pID'];
include "proddetail.php";
$plid = $explicitid;
} else {
$sSQL = "SELECT sectionID, rootSection FROM sections WHERE sectionURL='".sli_escape_string($url)."'";
$query = mysql_query($sSQL);
if ($query && mysql_num_rows($query)>0){
$rs = mysql_fetch_assoc($query);
$_GET['cat']=$rs['sectionID'];
$explicitid=$rs['sectionID'];
$secid = $explicitid;
if ($rs['rootSection']==1){
include "products.php";
} else {
include "categories.php";
}
} else {
include "content.php";
}
}
// For sections we want the .html
?>
And here is what I have unsuccessfully been trying to use to send the old dynamic link to the static page (there is no line break):
redirect 301 /proddetail.php?prod=cooling-hydration-backpack-system http://www.veskimo.com/cooling-hydration-backpack-system.html
I so appreciate any help you can give me. I admit I am a bit out of my depth with this.
Thanks so much,
Janell
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /proddetail\.php\?prod=cooling-hydration-backpack-system [NC]
RewriteRule ^ http://www.veskimo.com/cooling-hydration-backpack-system.html [L,R=301]

Part of url passed as variable in query string incorrectly includes their query string

I am in the process of moving a site to another server and I've ran into a problem.
The site uses .htaccess to redirect to index.php so the index.php can use $_SERVER['REQUEST_URI'] to load the whatever page is being called... However i cannot find where the old server has the .htaccess rules...so I am doing my own.
The .htacess rules I use are:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/?$1 [L,QSA]
the URL is newsiteurl.com/racintv/video_view?channel_id=2
and by printing out the params on the index.php page I am getting
Array ( [page] => racintv/video_view?channel_id=2 )
It should load the video_view page and pass channel_id (which loads a video playlist). This is not the case as it thinks the page to load is actually /video_view?channel_id=2
On the old site, I print out the params on the index.php page it prints
Array ( [page] => racintv/video_view )
and passes the ?channel_id=2 correctly as the video loads correctly
What am I missing in my .httacess that would pass ?channel_id=2 correctly?
Here is the PHP code which parses the URI
$path_info = trim(substr($_SERVER['REQUEST_URI'],1)) ;
$var_array = explode('/',$path_info);
$num_vars = sizeof($var_array);
if ($num_vars==1 or !($var_array[1])) {// in the form of '/foo' or '/foo/'
if ($var_array[0] && ! strstr($var_array[0],'index')) {
$var_array[1] = 'index'; // use index as the default page
$num_vars = 2;
}
}
if ($num_vars >= 2) { // in the form of '/foo1/foo2[/foo3 ...]'
$tmp_array = explode('.',$var_array[1]);
$var_array[1] = $tmp_array[0];
$page = $var_array[0] .'/'.$var_array[1];
$vars['page'] = $page;
$q_str = '';
for ($i=2;$i<$num_vars;$i++) {
$key = $var_array[$i];
$value = $var_array[++$i];
if ($key && $value) {
$vars[$key] = $value;
$q_str .= "$key=$value&";
$$key=$value;
}
}
}
//end of search engine friendly url parsing
The page parameter is missing:
RewriteRule ^(.+)$ /index.php/?page=$1 [L,QSA]

Resources