dongsu0308 2012-04-18 14:27
浏览 32
已采纳

mod_rewrite不同语言的url部分

Here is my current .htaccess:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-d [NC]

RewriteRule ^$ /launcher/index.html [L]

RewriteRule ^en/admin$ /main/index.php?lang=en&action=acp [L]
RewriteRule ^sp/admin$ /main/index.php?lang=sp&action=acp [L]

RewriteRule ^en/(.*)$ /main/$1?lang=en [L]
RewriteRule ^sp/(.*)$ /main/$1?lang=sp [L]

I have a launcher screen (which is working fine), and my site PHP files which is main/index.php. I use $_GET to pass information about what page to my index.php ie: index.php?action=download&file=123. There are two root levels I'm going to use /en/ for english, and /sp/ for spanish. These are passed to index.php as ?lang=en or ?lang=sp.

I have 2 problems that I cannot fix;

  • My rules have a strange inconsistency of failing. There's probably a better way of doing what I'm trying to do. I'd highly appreciate if someone can show me how. I'm a complete noob at mod_rewrite

  • When I add more things to the end of the URL using a ?variable=value, it occurs after ?lang=en or ?lang=sp. This means that the second question mark causes $_GET to glitch, and those $_GET passed variables are lost. I need a way to append two $_GET together with mod_rewrite?

  • 写回答

1条回答 默认 最新

  • duanliang4009 2012-04-19 11:15
    关注

    To solve your second problem, have you looked into using the QSA flag? Quoting the docs:

    'qsappend|QSA' (query string append)

    This flag forces the rewrite engine to append a query string part of the substitution string to the existing string, instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule.

    Regarding the first question, you can potentially combine the en and sp into a single rule like below. It is arguable whether this is an improvement, though.

    RewriteRule ^(en|sp)/(.*)$ /main/$2?lang=$1 [L,Q]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!