I was wondering if this was possible in mod_rewrite, and if anyone knows the syntax for it.
Browser receives:
http://example.com/video/my-first-vid/
mod_rewrite redirects to:
http://example.com/#/video/my-first-vid/
Thanks!
It seems you can't use mod_rewrite for redirecting to /#/video/my-first-vid/ as the # sign is urlencoded and it will redirect to http://example.com/%23/video/my-first-vid/ which obviously isn't what you're looking for. I tested this couple of days before on Apache 1.3.
You may create a separate script(s) for redirection. Add a new rules for this:
RewriteRule ^video/?$ /redirector.php?page=video [NC,L,QSA]
RewriteRule ^video/(.*)$ /redirector.php?page=video&subpage=$1 [NC,L,QSA]
But you might also need to parse the query string manually as it could be smth like:
/video/my-first-vid/?refID=test
Here's some simple example on PHP:
<?php
$page = (isset($_REQUEST["page"])) ? trim($_REQUEST["page"]) : "";
$subPage = (isset($_REQUEST["subpage"])) ? trim($_REQUEST["subpage"]) : "";
// setting the redirect URL, your conditions here
if ($page == "video")
{
$url = "/#/video/" . $subPage;
}
else
{
$url = "/"; // some default url
}
header("Location: " . $url);
exit;
?>
I'm not sure about embedding the '#' in the URI, since that's typically a fragment identifier, but you could try this:
RewriteRule "^(?<!#/)(.*)" "#/$1" [R=301,L]
That should redirect anything that doesn't have a '/#' in front of it to the same location with '/#' in front of it.
Related
I have this URL format:
https://www.example.com/shows/my_personal_show_2.0_34565.htm
Where the tailing number is the database reference, I need to rewrite it to:
https://www.example.com/shows/my-personal-show-2.0-my-personal-show.htm
We have thousands of indexed URLs by Google and need it to be rewritten via in .htaccess rules with 301 redirect to make it more SEO friendly without harming the SERP very much.
Thanks in advance.
Take SO as an example, the URL has this format:
https://stackoverflow.com/questions/48077576/support-with-rewrite-url-in-patterns
To make a SEO friendly URL, you can rewrite the URL to:
https://www.example.com/shows/34565/my-personal-show-2.0
To start, you can write your own router and map the above URL to the actual existing file:
.htacccess
RewriteRule ^shows/([0-9]+)/(.+)$ /router.php?id=$1&slug=$2 [QSA,L]
$1 is the back-reference to the regex part ([0-9]+) and $2 is the back-reference to the regex part (.+).
For the router.php, you can check if the given id and slug map to the existing htm file.
router.php
if (isset($_GET['id']) && isset($_GET['slug'])) {
$file_path = 'shows/' . str_replace('-', '_', $_GET['slug']) . '_' . $_GET['id'] . '.htm';
if (file_exists($file_path)) {
readfile($file_path);
}
else {
header("HTTP/1.0 404 Not Found");
}
exit;
}
The last thing is that you should make a permanently redirect of the SEO unfriendly URL to SEO friendly URL.
.htaccess
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/shows/(.+)\_([0-9]+)\.htm$
RewriteRule ^ /redirect.php?id=%2&slug=%1 [QSA,L]
redirect.php
if (isset($_GET['id']) && isset($_GET['slug'])) {
$redirectURL = '/shows/' . intval($_GET['id']) . '/' . str_replace('_', '-', $_GET['slug']);
header('location: ' . $redirectURL, true, 301);
exit;
}
Hi I want to change my current product URL structure to a new one but I want to keep my old ones working because of people who have linked to my products.
How can I do this without manually creating a 301 redirect for each product?
Not sure what Prestashop version you have, just tried in a 1.6.1.6
You can you this override:
<?php
class Dispatcher extends DispatcherCore
{
protected function loadRoutes($id_shop = null)
{
parent::loadRoutes($id_shop);
$language_ids = Language::getIDs();
if (isset($context->language) && !in_array($context->language->id, $language_ids)) {
$language_ids[] = (int)$context->language->id;
}
foreach ($language_ids as $id_lang) {
foreach ($this->default_routes as $id => $route) {
if($id == "product_rule")
$this->addRoute(
'product_rule2',
'{id}-{rewrite}{-:ean13}.xhtml',
'product',
$id_lang,
$route['keywords'],
isset($route['params']) ? $route['params'] : array(),
$id_shop
);
}
}
}
}
In this case I used a second rule for xhtml (without this override it gave a 404). You can change {id}-{rewrite}{-:ean13}.xhtml to the rule you have, then change the rules in SEO to want you want the new one to be.
Please write your ps version. If you use 1.5 or above?
Do you use this structure now {category:/}{id}-{rewrite}{-:ean13}.html ? or
{category:/}{rewrite}{-:ean13}.html or this {category:/}{rewrite}.html
please write more details
depends on it just make htaccess rule
([0-9]+) for id
(.*) for rewrite
and please use RedirectMatch permanent
and set to the result by $1, $2, etc
that guide must help to understand the htaccess url rewriting https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
I need advise for htaccess rule to decode url string for instance:
Actual Page is
http://www.mycarhelpline.com/index.php?option=com_latestnews&view=detail&n_id=953&Itemid=10
But Page is cached in Google as
http://www.mycarhelpline.com/index.php%3Foption%3Dcom_latestnews%26view%3Ddetail%26n_id%3D953%26Itemid%3D10
Decoded string will lead to correct url
I am sorry as is not at all aware of htaccess rule, hence not pasting code. Is there any htaccess rule to remove escaped characters and lead to correct url through redirect
Update
Like as given in
https://docs.joomla.org/J2.5:Developing_a_MVC_Component/Basic_backend
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import joomla controller library
jimport('joomla.application.component.controller');
// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');
// Get the task
$jinput = JFactory::getApplication()->input;
$task = $jinput->get('task', "", 'STR' );
// Perform the Request task
$controller->execute($task);
// Redirect if set by the controller
$controller->redirect();
Shall i add this line in get the task - will it do the needful
$url = urldecode($jinput->get('url', '', 'string'));
Final Code is
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
require_once( JPATH_COMPONENT.DS.'pager.cls.php' );
// import joomla controller library
jimport('joomla.application.component.controller');
// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');
// Get the task
$jinput = JFactory::getApplication()->input;
$task = urldecode($jinput->get('task', "", 'STR' ));
// Perform the Request task
$controller->execute($task);
// Redirect if set by the controller
$controller->redirect();
?>
HTACCESS COde - Though working Good Partially but is notconverting %3f to ?
RewriteCond %{QUERY_STRING} ^(.*)\?(.*)$
RewriteRule ^(.*)$ /$1?%1\%3F%2 [L]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ /$1\%3F%{QUERY_STRING}? [L,NE]
Update - Final HTACCESS
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ /$1?%{QUERY_STRING} [L,NE]
According to the tutorial, it is possible to handle all the requests for static pages through application/controllers/pages.php
class Pages extends CI_Controller {
public function view($page = 'home')
{
if( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('templates/nav', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/aside_right', $data);
$this->load->view('templates/bottom', $data);
}
}
This works for the "home"-page, but I don't seem to be able to call views/pages/about for example.
I tried to make a seperate controller for the about-page. It works, but it feels somewhat wrong.
application/controllers/about.php
class About extends CI_Controller {
public function index()
{
$this->load->view('templates/header');
$this->load->view('templates/nav');
$this->load->view('pages/about');
$this->load->view('templates/aside_right');
$this->load->view('templates/bottom');
}
}
I also have issues with my htaccess file or routes file. With the About-controller written above, I can only get to the page by typing domain.com/index.php/about. I'd like it to be domain.com/about etc.
This is what my routes.php's like:
$route['about'] = 'about';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
My Htaccess:
RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
As you already said, you don't need to use another controller for the about pages. Your problem is with your routes.php.
$route['about'] = 'about';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
In this way, it will search for a controller named "about" and it doesn't find the controller. If you erase the first line:
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
It should work. In this case, as you request any page, for example about it will call pages/view/about, where pages is the controller, view is the function and about is the argument that is passed to the function (replacing the default $page = home).
I also spotted another error in your logic. You wrote
This works for the "home"-page, but I don't seem to be able to call views/pages/about for example.
You don't have to call view/pages/about. You've to call pages/view/about. Remember that the syntax is always the same Controller/Function/Variable1/Variable2/Variable3. So, you should be able to see the about page with http://yourdomain.com/index.php/pages/view/about or only with http://yourdomain.com/about if you have the $route['(:any)'] = pages/view/$1 rules in your routes.php.
i've removed the index.php from my url but now i need to get some variable from the url , the CI solution is update the config.php
$config['uri_protocol'] = "PATH_INFO";
$config['enable_query_strings'] = TRUE;
this work perfectly when i use the index.php in the url , but i need it without index.php
this is my httacess
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA
any solution ? , thx
I had the same problem in CI, so I wrote this function to get the query strings from the $_SERVER['REQUEST_URI'] variable and extract them.
function extract_querystrings() {
$uri = $_SERVER['REQUEST_URI'];
$uri = explode('?',$uri);
if (count($uri)>1) {
foreach($uri AS $ur) {
$ur = explode('&',$ur);
foreach($ur AS $u) {
$u = explode('=',$u);
if ($u[0]=='/') continue;
$this->query_strings[$u[0]] = $this->input->xss_clean($u[1]);
}
}
}
//echo '<pre>',print_r($this->query_strings,true),'</pre>';
}
This function is called at my custom main controller's __construct().
You can modify the following line from
$this->query_strings[$u[0]] = $this->input->xss_clean($u[1]);
To
$_GET[$u[0]] = $this->input->xss_clean($u[1]);
And see if it works for you.
Have you looked at Elliot Haughin's Facebook library for CodeIgniter? It might provide some insights into using CI with Facebook.
http://www.haughin.com/code/facebook/