在Apache上使用mod_rewrite-语言参数 [英] mod_rewrite on apache - language parameter

查看:82
本文介绍了在Apache上使用mod_rewrite-语言参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在整个Internet上搜索了该问题的解决方案,阅读了有关该主题的各种主题和文章,但是确实为我找到了完整的解决方案-我认为mod_rewrite中的问题非常普遍.这是问题:

I have searched for a solution of this problem all over the Internet, read various threads and articles about it but did come to a full solution for my - i think quite generic problem in mod_rewrite. Here is the issue:

我开发了一个小型Web应用程序,可以让用户进行计算(假期后谁付钱给谁"的费用).现在,我要说的是要使其依赖于语言(尤其是静态页面).根据我的研究,通过传递get参数?lang =似乎没什么大不了,对于搜索引擎来说这似乎是一个问题-因此,将Apache mod_rewrite抢救起来拥有漂亮的URL

I have developed a small webapp that lets users make calculations (splitting costs after holidays "who pays to whom"). Now I have come to the point where I want to make it (especially the static pages) language dependent. What seemed like no big deal by passing a get parameter ?lang= seems to be a problem for search engines according to my research - so apache mod_rewrite to the rescue to have beautiful URLs like

example.com/en/index => example.com/index.php?lang=zh_CN.

example.com/en/index => example.com/index.php?lang=en.

example.com/en/about => example.com/about.php?lang=zh_CN.

example.com/en/about => example.com/about.php?lang=en.

此外,用户应该能够与朋友分享他们的计算-为此,他们会在计算后获得一个ID,因此像

Moreover, users should be able to share their calculations with their friends - for this they are issued an ID after caluclation therefore a rule like

example.com/c/9842398dfalkjdsf98sfdasf => example.com/c.php?id=9842398dfalkjdsf98sfdasf

example.com/c/9842398dfalkjdsf98sfdasf => example.com/c.php?id=9842398dfalkjdsf98sfdasf

用于调用其先前的计算(此处的语言处理直接在脚本中自动完成,也不需要在任何搜索引擎中为该链接建立索引).

is used to call their previous calculations (language handling is done here directly in the script automatically, also this links are not needed to be indexed in any search engine).

要达到这个目的,我想出了以下规则:RewriteEngine on

To achieve this I have come up with these rules: RewriteEngine on

RewriteRule ^c/([^/]+) c.php?id=$1 [L,QSA]

RewriteRule ^c/([^/]+)/ c.php?id=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([^/]+)/([^/]+)/$ $2.php?lang=$1 [L,QSA]

RewriteRule ^([^/]+)/([^/]+)$ $2.php?lang=$1 [L,QSA]

到目前为止,这些规则仍然有效-

So far these rules work - however:

这是我的问题:

1)对于依赖语言的网站,这种方法是一种好方法吗?这意味着google是否可以正确索引静态"网站(如关于"等)?2)有人提出了重写规则,也有类似

1) Is this approach a good approach for language dependent sites - meaning will google index the "static" sites like "about" etc. correctly? 2) Come somebody come up with a Rewrite Rule to also have requests like

example.com/about => example.com/about.php?lang=zh-CN.(请注意第一个网址中缺少的语言参数)将其发送为标准语言

example.com/about => example.com/about.php?lang=en. (notice the missing language parameter in the first url) to send them to the standard language

或者,然后我应该首先获取他们支持的语言,然后将其重定向到example.com/LANG/about

OR should I then first get their accpted langauge and then redirect them to example.com/LANG/about

3)我应该如何设计语言检测-特别是在主页上?现在,它可以按照上述规则工作-但是我在Internet上看到的解决方案首先将所有内容传递给index.php,然后将其像

3) How should I design the language detection - especially on the homepage? Right now it works according to the rules above - howver I have seen solutions on the Internet passing everything first to a index.php which then call the disred page like

index.php?lang = zh-CN& page = about

index.php?lang=en&page=about

当Google访问"它通常不会提供HTTP_ACCEPT_LANGUAGE,因此它是否还会看到其他语言版本(例如example.com/it/about)?

When google "visits" it will usually not provide an HTTP_ACCEPT_LANGUAGE so will it even ever see the other language versions like example.com/it/about ?

4)事实证明,使用RewriteRules会杀死代码中的相对CSS,JS,图片链接(惊奇!),但是我在互联网上找到了一个页面,说这也可以通过RewriteRule来处理,而不是使用绝对路径来处理.在HTML中?(

4) Turns out that using RewriteRules kill your relative CSS, JS, picture links in your code (suprise!), however I found a page on the internet saying that this also could be handled with a RewriteRule instead of using absoulute paths in the html? (here). Unfortunately I where not able to implement it.

简而言之:我有些困惑,希望有人可以帮助您建立一个简单的SEO一致性语言相关站点,并且这将帮助其他人整体上了解最佳实践解决方案.

In a nutshell: I am a little confused, hope somebody can help how to set up a simple SEO conform, language dependent site and that this will help others to see a best practice solution as a whole.

提前谢谢!

推荐答案

尝试一下,谢谢

RewriteEngine On

# This is to prevent the rules from looping, they only work as on-shot
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

# If the url is blank, go to 'web/en/'
RewriteRule ^/?$ /web/?lang=en [L,QSA]

# If the url starts with en,es,pt-br, remove it and add ?lang=$1 ,has /web
RewriteRule ^/?(en|es|pt-br)/web(/?.*)$ /web$2/?lang=$1 [L,QSA,R]

# If the url starts with en,es,pt-br, remove it and add ?lang=$1 ,has no /web
RewriteRule ^/?(en|es|pt-br)/?$ /web/?lang=$1 [L,QSA,R]

# If the url starts with en,es,pt-br, remove it and add ?lang=$1 ,everything else
RewriteRule ^/?(en|es|pt-br)/(.+?)/?$ /$2/?lang=$1 [L,QSA,R]

这篇关于在Apache上使用mod_rewrite-语言参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆