On my custom CMS I have a page with link like this:
www.mywebsite.com/index.php
Now I’m trying to use multilingual pages, so I’m testing on the index page. With MySQL and language switcher I’m successfully getting the content from the chosen language.
So, the link becomes like this on English and German:
www.mywebsite.com/index.php?lang=en
www.mywebsite.com/index.php?lang=de
With this htaccess code I’m removing the .php and index.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
So the link becomes like this:
www.mywebsite.com/?lang=en
www.mywebsite.com/?lang=de
For SEO purpose I want to remove the parameter ?lang=en or ?lang=de and put /en or /de, so the URL to look like this:
www.mywebsite.com/en
www.mywebsite.com/de
I’ve tried this with htaccess:
RewriteRule ^/(.+?)/(.*)$ $2?lang=$1 [L]
or this:
RewriteRule ^/(en|de)/(.*)$ $2?lang=$1 [L]
But when I try to enter the pages www.mywebsite.com/en or www.mywebsite.com/de, the content doesn’t change.
I guess I have a mistake in the htaccess code…
Also I want to do the same functionality on other pages, so let’s say this page:
www.mywebsite.com/posts?lang=en
to become
www.mywebsite.com/en/posts
2
Answers
To convert
http://example.com/file.php?lang=foo
tohttp://example.com/foo/file
you can use the following rules inroot/.htaccesso
:[Tested]
You are missing the
.php
in your new rewrite rules.It should be as follows:
As for the problem mentioned in comments (with CSS and JS files), you’ve found the correct solution to that, using:
This tells apache to not apply the RewriteRule if the request is directed to a real existing file or a directory.