Opencart Search page as SEO url - .htaccess

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]

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]

URL Rewrite Keeps redirecting back to xampp main page

I'm building a new version of a PHP website to have SEO linking URL
I'm really bad with REGEX and have very little understanding of how to work with .htaccess but I've been trying for two days to make a URL Rewrite rule, which I think is almost there but not quite yet.
In my .htaccess I have:
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ .php?id=$1 [QSA,L]
It's not just rewriting the end of the URL, it's redirecting to xampp main page. Please I'm really confused because I have tried several. Don't know if it's my hosting server that's the problem.
https://localhost/new%20owolab/?id=home
here's my index page:
/** Absolute path . */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
// Version
define('VERSION', '1.0.6');
// Check Version
if (version_compare(phpversion(), '5.3.0', '<') == true) {
exit('Please Upgrade To The Latest Version Of PHP From Version 5.3 + Required To Access This Site');
}
if (!ini_get('date.timezone')) {
date_default_timezone_set('UTC');
}
// Windows IIS Compatibility
if (!isset($_SERVER['DOCUMENT_ROOT'])) {
if (isset($_SERVER['SCRIPT_FILENAME'])) {
$_SERVER['DOCUMENT_ROOT'] = str_replace('\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0, 0 - strlen($_SERVER['PHP_SELF'])));
}
}
if (!isset($_SERVER['DOCUMENT_ROOT'])) {
if (isset($_SERVER['PATH_TRANSLATED'])) {
$_SERVER['DOCUMENT_ROOT'] = str_replace('\\', '/', substr(str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']), 0, 0 - strlen($_SERVER['PHP_SELF'])));
}
}
if (!isset($_SERVER['REQUEST_URI'])) {
$_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'], 1);
if (isset($_SERVER['QUERY_STRING'])) {
$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
}
}
if (!isset($_SERVER['HTTP_HOST'])) {
$_SERVER['HTTP_HOST'] = getenv('HTTP_HOST');
}
// Check if SSL
if (isset($_SERVER['HTTPS']) && (($_SERVER['HTTPS'] == 'on') || ($_SERVER['HTTPS'] == '1'))) {
$_SERVER['HTTPS'] = true;
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
$_SERVER['HTTPS'] = true;
} else {
$_SERVER['HTTPS'] = false;
}
require_once "header.php";
if(isset($_GET['id'])){
$id = $_GET['id'];
$filename = "layout/" . $id . '.php';
if(file_exists($filename)){
include $filename;
}else{
include "system/error.php";
}
}else{
include "layout/home.php";
}
require_once "footer.php";
?>````
I think this will get you the result you need :
New URL
https://localhost/new-owolab/?id=home
OR
https://localhost/newowolab/?id=home
Old URL
https://localhost/new%20owolab/?id=home

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