简体   繁体   中英

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM