【问题标题】:.htaccess Get variable as subdomain.htaccess 获取变量作为子域
【发布时间】:2015-11-15 13:31:31
【问题描述】:

我有一个需要手动翻译的网站。普通版是英文,翻译版是西班牙文。

可通过http://example.com/file.php?language=es 访问翻译版本(es 为espanol) 我希望域之后的 URL 不会因翻译更改而改变,所以我尝试改用 http://es.example.com/

这是我的 htaccess 的当前状态:

RewriteCond %{HTTP_HOST} !^example\.site$
RewriteRule home/$ /index.php [L,QSA]
RewriteRule contact-us/$ /contact-us.php [L,QSA]
RewriteRule our-location/$ /our-location.php [L,QSA]
RewriteRule gallery/$ /gallery.php [L,QSA]
RewriteRule (.*)/$ /user.php?display_name=$1 [L,QSA]

RewriteCond %{HTTP_HOST} !^es\.example\.site$
RewriteRule home/$ /index.php?language=es [L,QSA]
RewriteRule contact-us/$ /contact-us.php?language=es [L,QSA]
RewriteRule our-location/$ /our-location.php?language=es [L,QSA]
RewriteRule gallery/$ /gallery.php?language=es [L,QSA]
RewriteRule (.*)/$ /user.php?language=es&display_name=$1 [L,QSA]

!^example.site$ 下的第一组规则都覆盖了 !^es.example.site$ 下的规则,因此当我访问带有或不带有“es”子域的页面时,我会得到无论哪种方式都是英文版。如果我将子域规则(和重写条件)放在非子域规则之上,则会发生同样的事情,只是该站点仅翻译成西班牙语。

任何帮助将不胜感激。

编辑:

固定规则,感谢@hjpotter92的帮助

RewriteRule ^home/?$ /index.php [NC,QSA,S=3]
RewriteRule ^(contact-us|our-location|gallery)/?$ /$1.php [NC,QSA,S=2]
RewriteRule ^(?!user\.php).*/$ /user.php?display_name=$0 [NC,QSA]

RewriteCond %{HTTP_HOST} ^es\.example\.site$ [NC]
RewriteCond %{QUERY_STRING} !language=es [NC]
RewriteRule ^.*$ /$0?language=es [L,QSA]

【问题讨论】:

    标签: apache .htaccess mod-rewrite url-rewriting subdomain


    【解决方案1】:

    您可能正在尝试实现这一目标:

    RewriteEngine On
    
    RewriteRule ^home/?$ /index.php [NC,QSA,S=3]
    RewriteRule ^(contact-us|our-location|gallery)/?$ /$1.php [NC,QSA,S=2]
    RewriteRule ^(?!user\.php).*/$ /user.php?display_name=$0 [NC,QSA]
    
    RewriteCond %{HTTP_HOST} ^es\.example\.com$ [NC]
    RewriteCond %{QUERY_STRING} !language=es [NC]
    RewriteRule ^.*$ /$0?language=es [L,QSA]
    

    【讨论】:

    • 多么快速有效的回答啊!非子域规则的修订工作完美。但是由于某种原因,子域规则引发了 500 个内部服务器错误。
    • 当然。我收到了这样的重复行:“由于可能的配置错误,请求超出了 10 个内部重定向的限制。如有必要,请使用 'LimitInternalRecursion' 来增加限制。使用 'LogLevel debug' 来获取回溯。”
    • @awl19 再次更新了规则。请检查
    • 进行了更改。没有更多错误并且使用子域正确使用了 get 变量,但无论出于何种原因,继续域的“子文件夹”使用的是 user.php 规则而不是指定的规则。换句话说,es.example.com/home 真的出现了类似example.com/user.php?display_name=home&language=es 而不是example.com/index.php?language=es
    • @awl19 将规则中每个 S= 的值增加 1。我也会在我自己的服务器上运行一些测试。顺便说一句,您使用的是哪个 apache 版本?
    猜你喜欢
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多