Akash Mishra
Akash Mishra

Reputation: 189

skip subdirectory when replacing underscore to hyphen from urls using .htaccess code

I have to replace _ (underscore) with -(hyphen) from urls for the SEO purpose.I have hundreds of files so I can't change filenames one by one.So I wrote some .htaccess code to do that.

This is the code I am using.

RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L] 

It successfully changing my urls from underscore to hyphen.

Now I have one subdirectory named "administrator" and I dont want this directory urls to replace from underscore to hyphen.

So I want to skip urls of this directory to skip from .htaccess code.

Please help me how to skip this.

Thanks!

Upvotes: 2

Views: 235

Answers (2)

Croises
Croises

Reputation: 18671

You can also exclude separately:

RewriteCond %{REQUEST_URI} !^/administrator/
RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L] 

It's easier if you then need to exclude more than one directory, as it is just necessary to add the same line again.

Upvotes: 1

anubhava
anubhava

Reputation: 785276

You can use Negative lookahead:

RewriteRule ^(?!administrator/)([^_]*)_(.*)$ /$1-$2 [R=301,L,NC]

This will skip /administrator/ sub-directory from this rule.

Upvotes: 0

Related Questions