.htaccess and SEO friendly urls in a multilanguage website

Marco Gasi
Marco Gasi used Ask the Experts™
on
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")
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorFractional CTO
Distinguished Expert 2018

Commented:
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.
Marco GasiFreelancer
Top Expert 2010

Author

Commented:
Thank you David. I'm going to read that. :)
Marco GasiFreelancer
Top Expert 2010

Author

Commented:
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....
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Fractional CTO
Distinguished Expert 2018
Commented:
1) Your question title relates to "SEO friendly urls in a multilanguage website" + your setup fails to match Google's complex requirements for multilingual content organization... so...

If you're truly after high SEO traction (SERP - Search Result Placement), then best reorganize your content per Google's requirements.

Specifically, you can't really have a main language for your root content, if you expect to have high SERPs in Google.

All requests will select a language, then all languages will be organized the same, per Google's requirements.

Also, be sure every content page contains a correct <html lang="$lang"> at top of page, which matches selected language.

2) Change all 301s to 302s, because any redirect breakage will cache at your browser, so you will never see any changes.

3) After you've changed all 301s to 302s, flush your browser cache.

At this point you will be able to see .htaccess changes you make. Till you do this. You'll never see any changes you make.

4) Looks like you're running WordPress, so all your non-WordPress redirects must occur first, else WordPress will process all your requests before your custom rules + return either a404 for all your requests (if there's no matching WordPress post/page) or return a bare link (no additional of any query parameters if the page/post actually exists).

Note: For any non-WordPress rewrite rules to actually fire, you must place these ahead of your WordPress redirect processing + you must also remove completely the [L] flag, as your goal seems to be to add some query parameter, then have WordPress generate the correct content.

Note: This will be far easier to debug if you turn on deep debugging inside Apache + check your debug output.
Marco GasiFreelancer
Top Expert 2010

Author

Commented:
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
Marco GasiFreelancer
Top Expert 2010

Author

Commented:
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.
Marco GasiFreelancer
Top Expert 2010

Author

Commented:
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!
Marco GasiFreelancer
Top Expert 2010

Author

Commented:
Thank you again! :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial