.htaccess with url shortener - .htaccess

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]

Related

Codeigniter htaccess not working in ubuntu 16.04

This application biuld in Codeigniter Version = 3.1.3. when build this application work it properly with this htaccess file but today when i run this application & face this type of error.
When run my codeigniter website, htaccess file can't rewrite my
index.php url. so, requested URL was not found error shown.
properly working url => localhost/Magpie/index.php/Front_master/aboutus.html
Error url => localhost/Magpie/index.php/Front_master/aboutus.html
Here is my htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /Magpie/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Controller file: Front_master.php
<?php
class Front_master extends CI_Controller {
public $result = array();
public function __construct() {
parent::__construct();
//load models
$this->result["getContact"] = $this->GetInformation->getContact();
$this->result["getLogo"] = $this->GetInformation->getLogo();
$this->result["getSlide"] = $this->GetInformation->getSlide();
$this->result["getServices"] = $this->GetInformation->getServices();
$this->result["getContent"] = $this->GetInformation->getContent();
$this->result["getPropertyListing"] = $this->GetInformation->getPropertyListing();
$this->result["getBestDeal"] = $this->GetInformation->getBestDeal();
$this->result["getTeam"] = $this->GetInformation->getTeam();
}
public function index() {
$data = array();
$data['title'] = 'Home Page';
$data['header'] = $this->load->view('frontview/header', $this->result, TRUE);
$data['slider'] = $this->load->view('frontview/slider', $this->result, TRUE);
$data['dashboard'] = $this->load->view('frontview/dashboard', $this->result, TRUE);
$data['footer'] = $this->load->view('frontview/footer', '', TRUE);
$this->load->view('frontview/master', $data);
}
public function aboutus() {
$data = array();
$data['title'] = 'About Us';
$data['header'] = $this->load->view('frontview/header', $this->result, TRUE);
$data['dashboard'] = $this->load->view('frontview/about_us', $this->result, TRUE);
$data['footer'] = $this->load->view('frontview/footer', $this->result, TRUE);
$this->load->view('frontview/master', $data);
}
}
enter image description here
Use this htaccess code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /Magpie/
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
</IfModule>
Step 2 :
Remove index.php in codeigniter config
$config['index_page'] = '';
Step 3 :
Allow overriding htaccess in Apache Configuration (Command)
sudo nano /etc/apache2/apache2.conf
and edit the file & change to
AllowOverride All
for www folder
Step 4 :
Enabled apache mod rewrite (Command)
sudo a2enmod rewrite
Step 5 :
Restart Apache (Command)
sudo /etc/init.d/apache2 restart
and use this url:
localhost/Magpie/Front_master/aboutus
Sometimes this is will come from the database issue. The error will show "Expression #8 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'tappo_stage_db.Ratings.rating' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by".Therefore you should want to run this command.
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));

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]

mod_rewrite error

I'm using mod_rewrite in my new website.
.htaccess file
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rwr=$1
index.php file
if (isset($_REQUEST['rwr'])) {
if (substr($_REQUEST['rwr'], -1) == "/") {
$modrewrite = substr($_REQUEST['rwr'], 0, -1);
} else {
$modrewrite = $_REQUEST['rwr'];
}
$modrewrite = explode("/", $modrewrite);
}
if (isset($modrewrite) && $modrewrite[0] != "") {
$category = $modrewrite[0];
} else {
$category = null;
}
if (isset($modrewrite[1])) {
$service = $modrewrite[1];
} else {
$service = null;
}
if (isset($modrewrite[2])) {
$identification = $modrewrite[2];
} else {
$identification = null;
}
With this the link www.domain.com/service/webdesign/works go to -
$category = "service";
$service = "webdesign";
$identification = "works";
This works but I want to optimize it:
.htaccess file
RewriteEngine on
RewriteRule ^(.*)/(.*)/(.*)$ index.php?category=$1&service=$2&identification=$3
index.php file
$category = $_GET['category'];
$service = $_GET['service'];
$identification = $_GET['identification'];
But it doesn't work. Why? Can anyone help me?
Your rule should work fine for URL in question. It could be your Apache configuration and/or similarly named files that causing it. Lets take your example URL /service/webdesign/works. It is possible that you have file named service.php then Apache may ignore that rewrite rule for some reason.
You can use this approach:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# the rewrite rule
RewriteRule ^([a-z0-9\-_]*)(/([a-z0-9\-_]+))?(/([a-z0-9\-_]+))?$ index.php?category=$1&service=$3&identification=$5 [NC,QSA,L]
This is a tested and working solution.

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