在ASP.net中为多语言重写URL(URL rewrite in ASP.net for multi language)

编程入门 行业动态 更新时间:2024-10-18 16:46:05
在ASP.net中为多语言重写URL(URL rewrite in ASP.net for multi language)

使用示例网址:

www.domain.com/contact-us

哪个呈现English 。 本网站支持以下一组其他语言:

www.domain.com/es/contact-us www.domain.com/jp/contact-us www.domain.com/de/contact-us www.domain.com/pt/contact-us

这是英语的重写规则(默认语言)

<rewrite url="^/contact-us(\?(.+))?$" to="~/Pages/Contact.aspx$1" processing="stop"/>

我该如何修改/添加新规则来重写:

www.domain.com/jp/contact-us

至:

~/Pages/Contact.aspx?language=jp

最好不必为每个内容页面的每种语言编写新规则!

要使事情复杂化,需要匹配IETF语言标记。 这些变化很大,看起来像匹配它们的正则表达式将是一个困难的路线: https : //en.wikipedia.org/wiki/IETF_language_tag

理想情况下,我需要从数据库中获取语言列表,并在运行时匹配语言标记字段。 但我不知道怎么做,因为我只写过静态规则。

With the example URL:

www.domain.com/contact-us

Which renders in English. There are a set of other languages this website supports:

www.domain.com/es/contact-us www.domain.com/jp/contact-us www.domain.com/de/contact-us www.domain.com/pt/contact-us

Here is the re-write rule for English (default language)

<rewrite url="^/contact-us(\?(.+))?$" to="~/Pages/Contact.aspx$1" processing="stop"/>

How would I modify this/add a new rule to re-write:

www.domain.com/jp/contact-us

To:

~/Pages/Contact.aspx?language=jp

Preferably without having to write a new rule, for every language for every content page!

To complicate things, it needs to match IETF language tags. These are varied enough that it looks like a regex to match them would be a difficult route: https://en.wikipedia.org/wiki/IETF_language_tag

Ideally I need to get the list of languages from the database, and match the language tag field on the fly. But I'm not sure how to do this as I've only ever written static rules.

最满意答案

您是否可以访问主目录中的.htaccess文件? 如果是这样,您可以通过编写以下内容来制定自动重写规则

Options +FollowSymLinks RewriteEngine on RewriteRule /(.*)/contact-us/ /Pages/Contact.aspx?language=$1 RewriteRule /contact-us/ /Pages/Contact.aspx?language=en

让我解释:

在RewriteRule之后,用户友好的URL在左边,这里是/(.*)/contact-us/ ,其中(.*)是jp将发生的地方。 然后将jp转换到右边的真实URL,这里/Pages/Contact.aspx?language=$1替换$1 。

输入网址: http://www.example.com/jp/contact-us : http://www.example.com/jp/contact-us 输出网址: http://www.example.com/Pages/Contact.aspx?language=jp : http://www.example.com/Pages/Contact.aspx?language=jp language = http://www.example.com/Pages/Contact.aspx?language=jp

对于第二个RewriteRule ,由于URL http://www.example.com/contact-us/与第一个模型不匹配,它将变为http://www.example.com/Pages/Contact.aspx?language=en自动。

请记住.htaccess文件必须位于根目录(与Pages文件夹一起)。 否则你将无法获得所需的结果。

Do you have access to a .htaccess file in your home directory? If so, you can make an automatic rewrite rule by writing the following

Options +FollowSymLinks RewriteEngine on RewriteRule /(.*)/contact-us/ /Pages/Contact.aspx?language=$1 RewriteRule /contact-us/ /Pages/Contact.aspx?language=en

Let me explain:

After RewriteRule, the user-friendly URL is on the left, here /(.*)/contact-us/ where (.*) is where jp will take place. Then that jp is transposed on the real URL on the right, here /Pages/Contact.aspx?language=$1 to replace $1.

Input URL: http://www.example.com/jp/contact-us Output URL: http://www.example.com/Pages/Contact.aspx?language=jp

For the second RewriteRule, as the URL http://www.example.com/contact-us/ does not match the first model, it will turn into http://www.example.com/Pages/Contact.aspx?language=en automatically.

Please remember that the .htaccess file MUST be in the root directory (alongside Pages folder). Otherwise you will not get the desired result.

更多推荐

语言,language,contact-us,domain,电脑培训,计算机培训,IT培训"/> <meta name="

本文发布于:2023-04-28 02:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329755.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   中为   多语言   net   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!