Reputation: 973
I'm trying to rewrite dashes in URL so that they don't exist internally. For example this URL
localhost/mysite/about-me
"about-me" should be rewritten to "aboutme". I need this because the controller class name depends on this route string, and I obviously can't use dashes for this.
This is the condition and rule I found which I thought should fit my needs:
# Condition is to avoid rewrite on files within specified subdirs
RewriteCond $1 !^(css|img|ckeditor|scripts)
RewriteRule ^([^-]+)-([^-]+)$ $1$2 [L]
However it seems that it's not working, since the controller class Aboutme is not instanciated. I get a 404 error instead, and I don't have any problem with similar controller classes without a dash in their names.
Could you please give me a hand on this?
Upvotes: 1
Views: 163
Reputation: 11799
Here is a way to do it with mod-rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)([\w]*)-([\w]*)(.*)/?$ [NC]
RewriteRule .* %1%2%3%4 [L,DPI]
Won't redirect but can do it adding R=301
like this [R=301,DPI,L].
Does not have to be about-me
. Can be any words pair at any position. i.e.
localhost/mysite/about-me
= .../aboutme
or
localhost/mysite/folder1/folder2/folder3/my-folder
= .../myfolder
or
localhost/mysite/folder1/folder2/my-folder/folder3
= .../myfolder/...
Upvotes: 1
Reputation: 7902
You can extend the Router class.
In /application/core create a file called MY_Router.php (MY is the default prefix) and copy this into it;
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {
function set_class($class) {
$this->class = str_replace('-', '_', $class);
}
function set_method($method) {
$this->method = str_replace('-', '_', $method);
}
function _validate_request($segments) {
// Does the requested controller exist in the root folder?
if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).'.php')) {
return $segments;
}
// Is the controller in a sub-folder?
if (is_dir(APPPATH.'controllers/'.$segments[0])) {
// Set the directory and remove it from the segment array
$this->set_directory($segments[0]);
$segments = array_slice($segments, 1);
if (count($segments) > 0) {
// Does the requested controller exist in the sub-folder?
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $segments[0]).'.php')) {
show_404($this->fetch_directory().$segments[0]);
}
} else {
$this->set_class($this->default_controller);
$this->set_method('index');
// Does the default controller exist in the sub-folder?
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php')) {
$this->directory = '';
return array();
}
}
return $segments;
}
// Can't find the requested controller...
show_404($segments[0]);
}
}
This will automatically rewrite - to _ for you.
If you don't want underscores change the code to replace them with nothing;
all occurences of str_replace('-', '_',
to str_replace('-', '',
Upvotes: 1
Reputation: 474
Try removing ^ and $
# Condition is to avoid rewrite on files within specified subdirs
RewriteCond $1 !^(css|img|ckeditor|scripts)
RewriteRule ([^-]+)-([^-]+) $1$2 [L]
Upvotes: 1