Matt S
Matt S

Reputation: 91

htaccess change period to dash after subfolder

I have a tricky htaccess question if anyone can help…

I want to make it so that any periods ‘.’ are converted to dashes ‘-‘ for certain URLs (anything after a particular folder: www.mysite.com/folder).

So for example: www.mysite.com/hello.world will remain as www.mysite.com/hello.world BUT www.mysite.com/folder/hello.world will become www.mysite.com/folder/hello-world.

Sometimes there may be more than one period. Eg: www.mysite.com/folder/hello.world.2 should become www.mysite.com/folder/hello-world-2

To add one more layer of complexity: I cant access /folder/ so I can’t put the .htaccess file in there, it needs to be done from the root .htaccess file.

I’ve been searching a lot and cant find much on how to do this. If anyone can help with this I would really grateful!

Thanks in advance for any help.

Upvotes: 1

Views: 89

Answers (1)

anubhava
anubhava

Reputation: 785128

Place these 2 rules in your root .htaccess:

RewriteRule "^(folder)/([^.]*)\.+([^.]*\..*)$" /$1/$2-$3 [L,NC]
RewriteRule "^(folder)/([^.]*)\.([^.]*)$" /$1/$2-$3 [L,NC,R=302]

Upvotes: 1

Related Questions