0

I have a html page like http://test.com/en/about.html but if someone tries http://test.com/zh/about.html which is not available on server.

Is there any way to redirect it to http://test.com/en/about.html using .htaccess file. If yes how can write such rules.

Any help will be appreciated.

Many Thanks.

Amit Kumar
  • 3,384
  • 6
  • 25
  • 42

1 Answers1

1

If you want to replace a page with another, it is just a simple RewriteRule

RewriteRule ^zh/about.html$ /en/about.html [R,L]

If you want to redirect all pages to the English counterpart, you must first capture the part after the language, and append it to en. But you must be careful and check, if it is already en. Otherwise, you would have a rewrite loop

RewriteCond %{REQUEST_URI} !^/en/
RewriteRule ^../(.*)$ /en/$1 [R,L]

When everything works as it should, you may replace R with R=301 (permanent redirect). Never test with R=301.


To have any other length prefix, use .+? instead of ... This looks for one or more instead of just two characters

RewriteRule ^.+?/(.*)$ /en/$1 [R,L]
Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198