0
votes

I have changed my language code in URL from 3 to 2.So I want to redirect any old url to the new url through .htaccess

Example : I want to change the URL -

1) http://example.com/eng/News to http://example.com/en/News

2) http://example.com/deu/News to http://example.com/de/News

I am using CakePHP and this is the default .htaccess

     RewriteEngine on
     RewriteRule    ^$ app/webroot/    [L]
     RewriteRule    (.*) app/webroot/$1 [L]
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^(.*)$ index.php/$1 [L,QSA]

I tried using this :

      RewriteRule     (.*)/deu/(.*) $1/de/$2 [L]

But it didn't work. Can anyone help?

1

1 Answers

0
votes

Based on the URL examples you should use the following directives:

RewriteCond %{REQUEST_URI} ^/eng
RewriteRule ^/(.*)/(.*)$ /en/$2 [R]

RewriteCond %{REQUEST_URI} ^/deu
RewriteRule ^/(.*)/(.*)$ /de/$2 [R]

The first RewriteCond directive will check whether the URI starts from /eng. If yes, the corresponding RewriteRule will replace "eng" with "en" and append the rest of the URI, which is available in the parameter $2. The R flag will initiate a redirect so that the new URL will be visible in the browser. Similarly for deu.