0

I have a problem with my application. I use CodeIgniter for my project and it's forbidden to use hyphen in classname (underscores are allowed). But I want to separate words in URLs by hyphens. So I need to rewrite urls by a regex to replace underscores by hyphens in htaccess file. Ideas?

For example :

I have many links :

  • domain.tld/I_need_help

  • domain.tld/I_need_regex

I want to have :

  • domain.tld/I-need-help

  • domain.tld/I-need-regex

In htaccess file, there should be :

RewriteRule ^(.*)_(.*)$ /$1-$2
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Quentin L
  • 29
  • 8

3 Answers3

1

You are allowed to use routes to change url as you want.

$route['link-to_something'] = 'class_name/some_function';

after your edit: try this one:

RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
Arnas Pečelis
  • 1,000
  • 2
  • 12
  • 32
  • thanks for your answer. But I have many links and I must rewrite every link. You don't have a regex for all links ? – Quentin L Aug 31 '15 at 13:51
  • I'm not sure you need regex at all. Try to explain your problem including code or something – Arnas Pečelis Aug 31 '15 at 13:52
  • the one, hardcode way should be like changing your base_url function to change underscores to dashed before returning the result. This would work if you are using all your links with `base_url()` function – Arnas Pečelis Aug 31 '15 at 14:12
  • Euh ? I don't understand. My base_url() is empty but it should contain domain.tld. Not after domain.tld. – Quentin L Aug 31 '15 at 14:19
  • you should use all your links like `base_url('link-to/another-segment)`. – Arnas Pečelis Aug 31 '15 at 15:10
0

Ok after a long search. I have the solution :

Go to Codeigniter Routes regex - using dashes in controller/method names

Community
  • 1
  • 1
Quentin L
  • 29
  • 8
  • Remove the `R` flag. (Changing your links from `_` to `-` does seem to be backward? The hyphen is generally preferable.) – MrWhite Aug 31 '15 at 16:53
0

Why you are using regex when you can achieve this by changing just the one line?

Open application/config/routes.php and change

$route['translate_uri_dashes'] = TRUE;

That is it you need to do.

Usman Ahmed
  • 2,392
  • 1
  • 20
  • 17