I’m developing a web site that supports multiple languages (english and spanish) in PHP using parameters like this:
www.website.com/contact?lang=en
www.website.com/contact?lang=es
Yes, without extension .php…
I know that is a bad practice, so I want to have this:
www.website.com/en/contact
www.website.com/es/contact
I read other posts here in stackoverflow but, I don’t want to use a PHP framework, and I read that I have to use Mod Rewrite (again because I already use it for remove the extension). In fact, I read this post (and others too), but nothing works.
When it is done, I read in google pages that I have to use:
<link rel="alternate" href="http://www.website.com/" hreflang="x-default" />
is this correct?
Thanks
EDIT 1:
The content of my .htaccess is:
RewriteBase /
#Prevent directory listing
Options All -Indexes
#Disable Etags
Header unset ETag
FileETag None
#Prevent viewing of .htaccess
<Files ~ "^.*.([Hh][Tt][Aa])">
order allow,deny
deny from all
</Files>
RewriteEngine on
#Redirect non-www/www
RewriteCond %{HTTP_HOST} !^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#RewriteCond %{HTTPS} off
#RewriteCond %{HTTP_HOST} !^www.(.*)$ [NC]
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Options +FollowSymlinks -MultiViews
#Rewrite url
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^([^/]+)/([^/]+) /$2?lang=$1 [L,NC,QSA]
3
Answers
even if you use the solution from the post you marked out with a tiny adaption to meet your missing php file endings:
This should take care of the redirects. You’ll need to modify your links as @w3d has stated.
The
|
in the query string means ‘or’ if you want other languages you can add them in there or you could change it to the less safe.*
.This should be your full .htaccess with a new rule to redirect lang specific URLs: