ocpsoft / rewrite

OCPsoft URL-Rewriting Framework
http://ocpsoft.org/rewrite/
Apache License 2.0
189 stars 86 forks source link

Website migration - use rewrite to prevent 404 on old broken links #305

Closed djmj closed 3 years ago

djmj commented 3 years ago

When some of our clients move their webseite to our system many of their old links in google are broken and its not comfortable defining rewrite rules in apache client.

Is it possible to create rewrite rules just to redirect to a different URL? Basically a fallback for 404 pages since those old links would not work anymore. I tried to post in forum but its locked due to the spam.

Example:

Old URL from old website: /product/coca-cola-bottle.html New URL from our JSF rewritten website: /p/coca-cola-1-l/1000

We would save those old/new redirects in our database and load them in the rewrite config.

Is this possible?

.addRule()
.when(Path.matches("/product/coca-cola-bottle.html")) // page does not exists
.perform(Redirect.to("/p/coca-cola-1-l/1000")) // 301 redirect to existing page
lincolnthree commented 3 years ago

Hello, yes :) StackOverflow is probably the best place to ask general questions. But here you go!

https://github.com/ocpsoft/rewrite/blob/master/config-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/Redirect.java#L103

Let me know if that helps?

djmj commented 3 years ago

Hello, yes did not thought about posting at SO.

I will test this but looks promising!

djmj commented 3 years ago

I choose a @WebFilter for now with DispatcherType.ERROR since i want to look up the redirect target for the current request URL dynamically and was not sure how to do it with rewrite if not loading all mappings on start into the config. I think its also a cleaner solution than "abusing" rewrite for this.

https://stackoverflow.com/questions/70100287/jsf-url-migration-intercept-and-redirect-404-page-not-found-error