Belgin Fish
Belgin Fish

Reputation: 19837

htaccess regex question - 301 redirect spaces to dash

I'm just wondering what htaccess mod rewrite regex would be able to redirect any url that contains a space, to the same URL but a dash in replace of the space. For example

I'd want it to redirect any request going from

mysite.com/test/dl/1/the file name.html

to

mysite.com/test/dl/1/the-file-name.html

Is there any way you can do that?

Upvotes: 0

Views: 616

Answers (1)

LazyOne
LazyOne

Reputation: 165148

Yes you can, IF:

1) you hard-code such rule (means, you know the file name in advance):

RewriteRule ^test/dl/1/the\sfile\sname\.html$ /test/dl/1/the-file-name.html [R=301,L]

2) you can use RewriteMap and external rewriting program (Perl/bash/etc script) -- see Apache's manual for details (but I personally do not consider this as a very good option).

Otherwise you will have to do it yourself somehow (inside your own website script, for example).

Upvotes: 1

Related Questions