Frage

Currently I just have one language in my site,

And I implemented the friendly urls vía the .htaccess, like:

RewriteRule ^post/(.+)/(.+) post.php?id=$2&friendly=1

So:

domain.com is the homepage and domain.com/the-title/5 is the page for the post with ID 5.

Now I would like to make that as the default language urls, and for example, next language would be:

domain.com/es is the homepage and domain.com/es/the-title/6 is the page for the post with ID 6 in spanish. (but previous rule should work, too)

Question is,

How should I adapt my (or additional) rewrite rules to check for the 2 first chars of the url (first split) and add it as a param, like: &lan=es and if it's not found then don't add this parameter?

Lets say:

^post/(.+)/(.+) post.php?id=$2&friendly=1 (english)

^es/post/(.+)/(.+) post.php?id=$2&friendly=1&lan=es (spanish)

But if posible,

  • To just work with more languages (and add, if needed, the extra parameter),
  • To just work wit other rules, like:

^es/photo/(.+)/(.+) photo.php?id=$2&friendly=1&lan=es (spanish)

Any suggestions?

War es hilfreich?

Lösung 3

Here is what I would suggest:

RewriteRule ^/?((en|es)/)?post/(.+)/(.+)$ post.php?id=$4&friendly=1&lan=$2

Where /? allows optional forward slash at begining of string. This makes rule able to be moved interchangeably between htaccess directory contact and httpd.conf server context

((en|es)/)? Allows for optional specification of one of two accepted language codes.

Note that I did not suggest a wildcard for the language part, as I assume you are only working with a known subset of languages, so using something other than a known language code (or missing the entire thing) should fall through to handling be other rules (or perhaps result in 404).

If this is not the case you can change the first portion of the regex from (en|es) to (.{2}) if you expect exactly two characters, or perhaps (.{2}(-.{2})) if you expect to also handle language codes like es-ES.

Andere Tipps

Something like this might work. I haven't tested it but you can use RewriteCond to check for a specific structure of the uri and if it matches, use the following rule. If it doesn't then continue on to the original rule.

#Does the uri match 2 characters followed by /post/?
RewriteCond %{REQUEST_URI} ^../post/
#then use this rule and stop processing rules
RewriteRule ^(..)/post/(.+)/(.+) post.php?id=$3&friendly=1&lan=$1 [L]

#Else use this rule
RewriteRule ^post/(.+)/(.+) post.php?id=$2&friendly=1&lan=en

Edit: I added a default language to the end of the second rule. This way there is always a $_GET['lan'] parameter. You could leave it off and set a default in php. Your choice, no difference.

I can only answer you with advice cause we need more context...

  1. Use default pages to do a temporary redirect (302) to the default langauge or the user language.
  2. Use always the same scheme to get the language from the same pattern (http://mydomain.com/en/mypage.php)
  3. Use complete language codes if you will have a large public or for much content, like en_US, fr_FR, fr_CA ...
  4. Prefer negative search in your regex to avoid to capture the following characters, like "before/([^/]+)/after", in some cases, this is mandatory.
  5. If you don't have the language information, the user is not coming from a valid url, redirect him to a page with language informations (default or user language).
  6. If user is using direct php link, redirect him to the official link, to avoid duplicate content. You can use $_SERVER['REQUEST_URI'] to check it.
  7. Use a framework to manage it or at least a base to control the routes.

With these advices, you could use only the following rewrite rule for all your website:

RewriteRule ^([^\/]+)/([^\.]+)\.([\.]+)$ index.php?lang=$1&route=$2&format=$3 [L,QSA]

Here I capture the language (es, en, en_US, fr...), the route (post/5, gotabeer, cats/postit/thumb/2) and the format (html, json, jpeg...). (I didn't try the rewrite rule but it should work)

This should work for you:

RewriteEngine On

RewriteRule ^([a-z]{2})/post/([^/]+)/([0-9]+)/?$ post.php?id=$3&friendly=1&lan=$1 [L,QSA]

RewriteRule ^post/([^/]+)/([0-9]+)/?$ post.php?id=$2&friendly=1&lan=en [L,QSA]
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top