0

I have a multilingual web application that depends on a language code being in the URL.

Example:

example.com/zh-hans/title

The client now wants a new structure:

example.com/cn/title

The problem is the application was built around using zh-hans. As that part of the URL is an argument that is necessary for the application to work properly, changing it would break the application. Also, it's not possible to change this in the application to accommodate. This would take 100s of hours of work to undo.

Is it possible for the browser to show one URL while behind the scenes data is served from the other URL and thus preserve functionality?

The Apache documentation state that:

Assume we have recently renamed the page foo.html to bar.html and now want to provide the old URL for backward compatibility. However, we want that users of the old URL even not recognize that the pages was renamed - that is, we don't want the address to change in their browser.

https://httpd.apache.org/docs/2.4/rewrite/remapping.html (From old to new (internal))

Then, would the solution to this issue be as simple as this?

RewriteEngine  on
RewriteRule    "^/zh-hans/(.+)"  "/cn/$" [PT]
MrWhite
  • 11,643
  • 4
  • 25
  • 40
user658182
  • 135
  • 7
  • Possible duplicate of [Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod\_Rewrite Rules but Were Afraid to Ask](http://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever) – Tero Kilkanen Mar 21 '17 at 15:25

1 Answers1

0
RewriteEngine  on
RewriteRule    "^/zh-hans/(.+)"  "/cn/$" [PT]

If the "client now wants a new structure: example.com/cn/title" then it looks like your rewrite directive should be the other way round? For example:

RewriteRule    ^/cn/(.+)  /zh-hans/$1 [PT]

You still need to change the visible URL structure in your application to show example.com/cn/title (after all, the client "wants a new structure") which then gets internally rewritten to example.com/zh-hans/title, so that your underlying application can understand it.

However, it is still dependent on your web application reading the URL as intended. If, for instance, it looks at the REQUEST_URI (or equivalent) then it's still going to see example.com/cn/title.

MrWhite
  • 11,643
  • 4
  • 25
  • 40
  • I've reviewed my rewrite rule structure. I'm a bit confused as to the change you've proposed. On that example page in the docs for Apache, 'foo.html' is the old url. 'bar.html' is the new url. Because zh-hans is the old url and cn is the new, wouldn't they be on left and right size respectively? Or are you saying that cn needs to "Pass Through" to zh-hans in order for things to work as expected? – user658182 Mar 21 '17 at 16:12
  • In that example on the Apache docs the underlying filesystem has changed (a file is renamed), but the visible URL remains the same. The new _filename_ is `bar.html`. The old _filename_ was `foo.html` (which was also, and still is, the URL). You've stated that the "client now wants a new [URL] structure" - you can only do that by changing the visible URL structure. (Unless the "new structure" your client talks of is the underlying _file structure_? But I don't think that is the case here?) But your app is expecting `zh-hans`, not `cn`, hence the rewrite ("for things to work as expected"). – MrWhite Mar 21 '17 at 16:27
  • It's not necessarily a "quick fix". Changing the URL structure also requires external redirects for all indexed URLs etc. I'm kind of wondering whether this is going to be possible... if you are unable to "change the application to accommodate" a change in the URL, how are you going to change the URL? – MrWhite Mar 21 '17 at 16:31