Link to home
Start Free TrialLog in
Avatar of Marco Gasi
Marco GasiFlag for Spain

asked on

.htaccess and SEO friendly urls in a multilanguage website

Hi everybody.
In a website I use .htaccess to write SEO friendly url and in the root directory everything works fine.

.htaccess
#first I drop the file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} ^(.+)\.php$
RewriteRule (.*)\.php$ /$1 [R=301,L]
#RewriteRule ^(.*)$ $1.php [QSA]

# then redirect "/vinos.php?id=xxx" to "/vinos/xxx"
RewriteCond %{THE_REQUEST} \s/vinos\.php\?v=(\w+)\s [NC]
RewriteRule ^ /vinos/%1? [R=301,L]

# internally rewrite "/vinos/xxx" to "/vinos.php?id=xxx"
RewriteRule ^vinos/(\w+)$ /vinos.php?v=$1 [L]

Open in new window


This way, the url mywebsite.com/vinos.php?v=sangre_de_toro becomes mywebsite.com/vinos/sangre_de_toro.
So in the vinos.php I have:
$url = explode("/", ($_SERVER["REQUEST_URI"]));
$vino_querido = end($url);

Open in new window

and then I use the variable $vino_querido in my sql query.

But I have added the support for two other language replicating the website pages in two subfolders 'en' and 'it' (the main langage 'es' is in the root). But in these subfolders the rewriting doesn't work.
The pages have different names, so I have added additional rules to my .htaccess:
RewriteCond %{THE_REQUEST} \s/it/vini\.php\?v=(\w+)\s [NC]
RewriteRule ^ /it/vini/%1? [R=301,L]

RewriteRule ^it/vini/(\w+)$ /it/vini.php?v=$1 [L]

RewriteCond %{THE_REQUEST} \s/en/wines\.php\?v=(\w+)\s [NC]
RewriteRule ^ /en/wines/%1? [R=301,L]

RewriteRule ^en/wines/(\w+)$ /en/wines.php?v=$1 [L]

Open in new window

But this has no effect, the url is not rewritten and the last element of the array $url in the wines.php (and vini.php) is "wines?v=blanco_afrutado" (or "vini?v=blanco_afrutado")
Avatar of David Favor
David Favor
Flag of United States of America image

Tip: If you expect any SEO juice from Google, then you must follow their instructions, to the letter, for best SEO.

You can do anything you like + if you'd like to keep your life simple, just follow Google's instructions.

https://support.google.com/webmasters/answer/189077?hl=en + https://support.google.com/webmasters/answer/182192?hl=en provide good starting points.

Best to read many Google articles about this before starting.

The search - multilingual site google webmaster - provides a good starting point.
Avatar of Marco Gasi

ASKER

Thank you David. I'm going to read that. :)
Mmmmm, still have to do a deep search, but the two links you suggested, although useful, are not related at all with.my technical question....
ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi David thank you for the explanation. I'll follow your suggestions carefully. I'll let you know as soon as possible.

BTW: no I'm not running WordPress. :)
Cheers
Hi David.
I have resturctured my website follwing Google lineguides and your suggestions. But this didn't solve my issue. But these lines of .htaccess code
RewriteCond %{THE_REQUEST} \s/es/vinos\?v=(\w+)\s [NC]
RewriteRule ^ /es/vinos/%1? [R=302,L]

Open in new window

don't work.
When the main langiage was in the root they looked as in original question
RewriteCond %{THE_REQUEST} \s/vinos\.php\?v=(\w+)\s [NC]
RewriteRule ^ /vinos/%1? [R=301,L]

Open in new window

and they worked great.

Now, I would like how I can make them work even for subfolders... Any idea?
Thank you so much for your help.
Solved! I shluld just drop the extension from the RewriteCond, replacing this:
RewriteCond %{THE_REQUEST} \s/vinos\.php\?v=(\w+)\s [NC]

Open in new window

with this
RewriteCond %{THE_REQUEST} \s/vinos\?v=(\w+)\s [NC]

Open in new window

Now everything works fine and thanks to your help it should also be compliant wioth Google guidelines.
Thank you!
Thank you again! :)