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;
}
Related
Hi i am new to codeignitor my application works fine in my local host but when i bring it live server it gives me the below error
Error after login
My config file i have already set the base_url to
$config['base_url'] = 'http://diamondglass.com.sg//portal/';
and
$config['index_page'] = '';
My .htaccess is in my portal folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
My index function in my project Controller
function index()
{
$data['projects'] = $this->project_model->getAll();
$data['users'] = $this->user_model->getAll();
// set array of items in session for page
$arraydata = array(
'mainsection' => 'Projects',
'subsection' => 'Projects'
);
$data['statuscount'] = $this->project_model->getStatusCount();
$this->session->set_userdata($arraydata);
$this->load->view('admin/project/index',$data);
//echo '<pre>';
//print_r($this->session->userdata());
}
My Login Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
if($this->session->userdata('user'))
redirect('admin/project');
$this->load->helper(array('form'));
$this->load->library(array('form_validation'));
}
public function index()
{
$this->load->view('login');
}
//To Verify Logins
function verify()
{
$this->load->model('user_model');
/* Load form validation library */
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run() == TRUE)
{
//model function
$this->load->model('user_model');
$check = $this->user_model->validate();
if($check)
{
$this->session->set_userdata('user','1');
$this->session->set_userdata('currentUser',$check);
redirect('admin/project');
}
else
{
$this->session->set_flashdata('error', 'Invalid Username and Password');
redirect('login');
}
}
else
{
//false
redirect('login');
}
}
}
I have been searching the web high and low for a solution, but i can figure this out. I know it got to do with .htaccess but can't figure this out. can someone help
IMHO, your base_url has wrong (// after .sg)
Second, make sure your view-file is located at: ../Views/admin/project/index.php
You don't have to put the IfModule tags every time. Use this code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ErrorDocument 404 http://diamondglass.com.sg/404.html
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ <YourRelativePathToPHPFile>/404.html [L]
Make sure you have a custom 404 page
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 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.
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/