Rectangle 27 0

RewriteCond     %{REQUEST_URI}     ^/listing-(\w+-)+\d+\.html
RewriteRule     ^listing-(.*)      $1

.htaccess - Regex htaccess rewrite to strip url prefix - Stack Overflo...

regex .htaccess redirect http-status-code-301
Rectangle 27 0

from flask import Flask, g, render_template, redirect, request


app = Flask(__name__)

default_language = 'en'
language_urls = {
    'en': 'mysite.com',
    'fr': 'mysite.com/fr',
    'ru': 'mysite.ru',
    'by': 'mysite.ru/by',
}
languages = ','.join(language_urls.keys())


def get_language_by_request(request_host, request_path):
    '''
    Looking bad, but work.
    I cab't use request.view_args there,
    because this can't detect language for 404 pages
    like mysite.com/fr/unknown-page
    '''
    request_host_path = request_host + request_path
    request_paths = request_host_path.split('/', 2)
    if (len(request_paths) > 1 and request_paths[1] in language_urls.keys()):
        request_language_prefix = request_paths[1]
        return request_language_prefix
    for language, url in language_urls.items():
        host_prefix = url.split('/')
        if len(host_prefix) == 1:
            host, = host_prefix
            if request_host == host:
                return language
    return default_language


def get_language_url_parameter_value(language, request_host):
    host_prefix = language_urls[language]
    if host_prefix == request_host:
        return None
    return language


def get_redirection_url_by_request(request_host, request_path, request_url):
    '''
    Looking bad, but work.
    I cab't use request.view_args there,
    because this can't detect language for 404 pages
    like mysite.com/fr/unknown-page
    '''
    request_host_path = request_host + request_path
    request_paths = request_host_path.split('/', 2)
    request_language_prefix = None
    if (len(request_paths) > 1 and request_paths[1] in language_urls.keys()):
        request_language_prefix = request_paths[1]
    hosts = []
    for language, url in language_urls.items():
        host_prefix = url.split('/')
        if len(host_prefix) == 1:
            host, = host_prefix
            language_prefix = None
        else:
            host, language_prefix = host_prefix
        if request_host == host and request_language_prefix == language_prefix:
            return None
        hosts.append(host)
    if request_host not in hosts:
        return None
    if request_language_prefix:
        request_host_prefix = request_host + '/' + request_language_prefix
        host_prefix = language_urls[request_language_prefix]
        return request_url.replace(request_host_prefix, host_prefix)
    return None


@app.url_defaults
def set_language_in_url(endpoint, values):
    if '_lang' not in values and hasattr(g, 'language_url_value'):
        values['_lang'] = g.language_url_value


@app.url_value_preprocessor
def get_language_from_url(endpoint, values):
    g.language = get_language_by_request(request.host, request.path)
    g.language_url_value = get_language_url_parameter_value(g.language, request.host)
    if values and '_lang' in values:
        del values['_lang']


@app.before_request
def check_language_redirection():
    redirection_url = get_redirection_url_by_request(request.host, request.path, request.url)
    return redirect(redirection_url) if redirection_url else None


@app.route('/')
@app.route('/<any(%s):_lang>/' % languages)
def home():
    return render_template('home.html')


@app.route('/other/')
@app.route('/<any(%s):_lang>/other/' % languages)
def other():
    return render_template('other.html')

I don't use blueprints there because I also use flask-login and I can't set several login pages with different languages for each blueprint. For example if page required login, flask redirect me to login page and I must update language for this page. Also login pages can't be as mysite.com/login, mysite.com/fr/login and etc without several redirections.

UPD: I can't use request.view_args for detect language or redirection, because on this case I can't detect language for error pages as mysite.com/fr/wrong-page-there (can't detect endpoint and view_args). To avoid this problem I can use hask: add url rule as /<lang_code>/<path:path> and raise 404 error there.

url routing - Python flask web application with multilanguages support...

url-routing flask multilanguage custom-error-pages
Rectangle 27 0

The most efficient way would be through your web server. You can easily check if the request is for / (the home page) and redirect to /en.

Check the docs for what ever web server you are using, they all have something like mod_rewrite or similar.

You could set up a route like /set_default_language to redirect to in case of /, this controller can access the db and do what ever it needs.

Alternatively you can make it redirect to /your/usual/language_switch with no language specified and allow the code to use the default.

Forcing language prefix on URL in CakePHP - Stack Overflow

cakephp cakephp-2.3
Rectangle 27 0

If you don't want to redirect the requestAction calls from within your controllers or views you should add the following condition to your IF statement

if (empty($this->request->params['language']) &&
    empty($this->request->params['admin']) && 
    empty($this->request->params['requested'])) {
    ...
    }

Forcing language prefix on URL in CakePHP - Stack Overflow

cakephp cakephp-2.3
Rectangle 27 0

Are www.domain.com/fr_FR/my_action and www.domain.com/de_DE/my_action the same resource, just represented in different languages? Or would your users see different contents depending on the language they choose (like, I don't know, different news)?

If the answer to the first question is yes I'd rather go for implementing Accept-language header compliance, for example using I18N::AcceptLanguage, which has the additional benefit that it won't interfere in any way with how you designed your URLs.

perl - Catalyst best way for url language prefix? - Stack Overflow

perl internationalization multilanguage catalyst
Rectangle 27 0

You can use this rule in your root .htaccess of .com and .de sites:

RewriteEngine On

RewriteRule ^de/(.*)$ http://mysite.de/content/$1 [L,NC,R=301]

regex - How to detect/redirect URL with language prefix - Stack Overfl...

regex .htaccess mod-rewrite redirect http-status-code-301
Rectangle 27 0

An easier way might be to store the chosen language in a cookie and then not have to rewrite all the URLs. You could also potentially detect the user's browser language automatically.

However, search engines would be unlikely to pickup the various languages and you'd also lose the language if someone tried to share the link.

But love the full solution you posted, very comprehensive, thanks. :-)

The problem with "URL-less" language switches is search engines. They will only ever index a single language or will get their panties in a bunch if they see different content on the same URL. That only really works for private (login-only) areas of your site.

routing - Adding a prefix to every URL in CakePHP - Stack Overflow

cakephp routing prefix
Rectangle 27 0

Looking at Drupal core code (in example, node_menu()), menu callback using menu placeholders are not adjusted to work when locale.module is enabled, nor local.module alters the menus defined from other modules. In fact, language_initialize(), called on Drupal bootstrap contains the following code:

case LANGUAGE_NEGOTIATION_PATH_DEFAULT:
case LANGUAGE_NEGOTIATION_PATH:
  // $_GET['q'] might not be available at this time, because
  // path initialization runs after the language bootstrap phase.
  $args = isset($_GET['q']) ? explode('/', $_GET['q']) : array();
  $prefix = array_shift($args);
  // Search prefix within enabled languages.
  foreach ($languages as $language) {
    if (!empty($language->prefix) && $language->prefix == $prefix) {
      // Rebuild $GET['q'] with the language removed.
      $_GET['q'] = implode('/', $args);
      return $language;
    }
  }

The code is removing the language ID passed in the URL. If locale.module is correctly set, the menu callback definitions should not be changed when the module is enabled.

drupal - Locale module and language prefix in url - Stack Overflow

drupal drupal-6 drupal-modules hook-menu
Rectangle 27 0

You can use root .htaccess with:

RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteCond %{REQUEST_URI} !^/(?:cz|en|sk)/
RewriteRule ^([^/]+(/.*)?)$ /cz/$1 [R=301,L]

yes, but the /forum link was just an example. I have 100's of URL for default language /category1 /category2 an so on.

OK!!!!! :-) I edit the code for that

Thank you for sharing this, nevertheless it does not work for me. The rule creates infinitive loop every time except the case when cz|en|sk is part of the URI. The loop contains something like: /usr/www/users/betatn/cz/

You copy-paste this code, and use only that in your root .htaccess ? Because it redirect to cz... I just add one / now.

RewriteCond %{REQUEST_URI} !^/index\.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php [L]

.htaccess - How to use rewrite condition to redirect when language pre...

.htaccess mod-rewrite joomla3.0
Rectangle 27 0

Thasks for useful link, now I have solution very similar for this, but see bottom of my own answer why I don't use blueprints and request.view_args for language detection: stackoverflow.com/a/14958893/880326.

url routing - Python flask web application with multilanguages support...

url-routing flask multilanguage custom-error-pages
Rectangle 27 0

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^listing-(.+?\.html)$ /$1 [L,R=301,NC]

Thanks very much, just the ticket :)

You're welcome, glad that it worked out.

.htaccess - Regex htaccess rewrite to strip url prefix - Stack Overflo...

regex .htaccess redirect http-status-code-301