【问题标题】:htaccess reposition a part of a urlhtaccess 重新定位 url 的一部分
【发布时间】:2015-06-21 18:15:59
【问题描述】:

您好,我有一个应该提供多种语言的网站。我用wordpress做的。我想要实现的是每种语言都有自己的(虚拟)子域。例如 fr.foo.eu 和 nl.foo.eu。

目前我有以下格式的网址: foo.eu/?lang=nl foo.eu/?lang=fr

网址中可能有更多内容,例如: http://hypnose.eu/hallo-wereld/?lang=nl

我想通过以下方式使用 htaccess 获取我的网址:

 http://nl.hypnose.eu/hallo-wereld/

你们能帮我实现这个目标吗?

这是我目前在 .htaccess 中的内容:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^([a-z]{2}\.hypnose\.eu$ [NC]
RewriteCond %{THE_REQUEST} \ /+([^\?]*)\?lang=([a-z]{2})&?([^\ ]*)
RewriteRule ^ http://%2.hypnose.eu/%1?%3 [L,R]

RewriteCond %{QUERY_STRING} !(^|&)lang=
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.hypnose\.eu$ [NC]
RewriteRule ^(.*)$ /$1?lang=%2 [L,QSA]

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]
</IfModule>

【问题讨论】:

  • 您能否编辑您的问题并添加您目前拥有的.htaccess?那太好了,所以我们可以提供一些帮助。谢谢!
  • @milz 是的,我现在添加了它

标签: wordpress .htaccess mod-rewrite


【解决方案1】:

尝试在您的 wordpress 规则之上添加:

RewriteCond %{HTTP_HOST} !^([a-z]{2})\.foo\.eu$ [NC]
RewriteCond %{THE_REQUEST} \ /+([^\?]*)\?lang=([a-z]{2})&?([^\ ]*)
RewriteRule ^ http://%2.foo.eu/%1?%3 [L,R]

RewriteCond %{QUERY_STRING} !(^|&)lang=
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.foo\.eu$ [NC]
RewriteRule ^(.*)$ /$1?lang=%1 [L,QSA]

【讨论】:

  • 感谢您的回复。但这似乎不起作用,网址仍然保持不变。我在 RewriteBase / 之后添加了您给我的行。我删除了规则:RewriteRule ^/nl/.任何其他想法我做错了什么?
  • @Jantje 您输入的是什么 URL,您希望发生什么。当您“重写”时,就是在浏览器的地址栏中输入类似:http://nl.hypnose.eu/hallo-wereld/ 的 URL,然后规则将其“重写”为:/hallo-wereld/?lang=nl
  • 例如我希望:foo.eu/hallo-wereld/?lang=fr 在地址栏中显示为fr.foo.eu/hallo-wereld。但它必须显示foo.eu/hallo-wereld/?lang=fr 的内容。可能我不够清楚,抱歉
  • @Jantje 那么你要求 2 条规则,一条用于重定向浏览器,一条用于重写请求,请参阅编辑
  • 我添加了您的新帖子,但现在我收到 500 内部服务器错误。我还添加了我当前的 .htaccess 文件。我是否需要设置其他内容,例如子域别名?
【解决方案2】:

有流行的多语言文字插件插件,可以设置当前语言的 URL

1)qTranslate X

2)qTranslate

在插件中尝试预域模式。

【讨论】:

  • 谢谢你,我知道我目前已经安装了 qTranslate X,但是当我使用域前功能时我遇到了同样的问题。出现内部服务器错误 500。
猜你喜欢
  • 1970-01-01
  • 2017-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多