Home > Blockchain >  htaccess 301 redirect url without subdirectories
htaccess 301 redirect url without subdirectories

Time:12-13

I'm trying to redirect this url:

https:///www.domain.com/courses/company/

to this one:

https:///www.domain.com/courses/

without also redirecting these:

https:///www.domain.com/courses/company/123/ https:///www.domain.com/courses/company/456/

I first tried this:

Redirect 301 /courses/company/ /courses/

but that also redirected the urls with the subdirectories.

Next I tried this:

Redirect 301 /courses/company/$ /courses/ ...because I thought the $ sign makes it match exactly that url.

But it didn't redirect anything.

Thanks!

CodePudding user response:

The issue with the Redirect directive is that it does not allow to use "regular expressions" as you tried to do. It only implements a simple prefix string matching approach.

Instead you'd have to use the RedirectMatch directive which does support such expressions:

RedirectMatch 301 ^/courses/company/?$ /courses/

See the documentation of the RedirectMatch directive.

Instead you could also use the more flexible rewrite module:

RewriteEngine on
RewriteRule ^/?courses/company/?$ courses/ [L,R=301]

And a general hint: it usually is a good idea to start out with a 302 temporary redirection. And to only change that to the 301 permanent redirection once everything works as intended. That prevents nasty caching issues for your clients.

  • Related