0

I'm trying to escape everything until the last slash, even if there's a slash in between. Example:

/site_url/courses/cat1/cat2/subcat/coursename

to

/site_url/course/courseName 

or

/site_url/course/courseName 

How could I achieve this with Nginx or Apache?

farroh
  • 3
  • 1
  • 1
    "escape everything" - It looks like you want to _remove_ everything? Is `site_url` not necessarily the same in the source/target? Although your example also appears to be capitalising the `n` in `coursename`?! "to `/site_url/course/courseName` or ..." - Your 2 _alternatives_ appear to be the same? Do you have any other directives in your Apache `.htaccess` file? – MrWhite Nov 01 '21 at 15:45

1 Answers1

0

If by "escape", you mean "remove" (as in your example) then you can do something like the following in your Apache config (or .htaccess) file:

RedirectMatch 301 ^(?!/site_url/course/).+/([^/.]+)$ /site_url/course/$1

The negative lookahead (ie. (?!/site_url/course/)) ensures that the redirected request is not also redirected (which would cause a redirect-loop).

This is assuming you are not already using mod_rewrite.

And this obviously doesn't do anything to capitalise any letters, since your example appears to go from /coursename to /courseName, which is not a discernable pattern (unless this is literal text?!).

Test with a 302 (temporary) redirect to avoid potential caching issues.

MrWhite
  • 12,647
  • 4
  • 29
  • 41