Score:0

htaccess match all words until last slash and redirect 301

cn flag

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?

kz flag
"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?
Score:0
kz flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.