网址重写并获取参数

时间:2017-10-03 18:38:40

标签: .htaccess

我想重写我的网址:

从这个: https://example.com/try

对此: https://example.com/en/try

在此期间,我需要为FR内容提供条件,其中网址应为:https://example.com/fr/essayer

所以我实际上有这段代码:

RewriteRule en/login login.php?lang=en [L,QSA]
RewriteRule fr/connexion login.php?lang=fr [L,QSA]

它看起来很有效但我无法检索lang参数。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

来自您的https://jsfiddle.net/xpgjyejn/代码。我看到你写了这两行来删除扩展名(即,内部重定向到扩展名为.php的文件)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

所以当用户搜索

/en/brand

内部会搜索

/en/brand.php

它有L标志并且它会停止执行。因此,该行旁边的代码都没有被执行。正如您已经在.htaccess文件中编写了所有重写规则,通过添加#infront来注释掉这两行

   # RewriteCond %{REQUEST_FILENAME} !-f
   # RewriteRule ^([^\.]+)$ $1.php [NC,L]

现在你的代码将是

 RewriteEngine On

    # Removing extension
    #RewriteCond %{REQUEST_FILENAME} !-f
    #RewriteRule ^([^\.]+)$ $1.php [NC,L]

    # Redirect rewrite urls
    # brand
    RewriteRule en/brand brand.php?lang=en [L,QSA]
    RewriteRule fr/marque brand.php?lang=fr [L,QSA]

    # conditions
    RewriteRule en/conditions conditions.php?lang=en [L,QSA]
    RewriteRule fr/conditions conditions.php?lang=fr [L,QSA]

    # contact
    RewriteRule en/contact contact.php?lang=en [L,QSA]
    RewriteRule fr/contact contact.php?lang=fr [L,QSA]

    # features
    RewriteRule en/features features.php?lang=en [L,QSA]
    RewriteRule fr/fonctionnalites features.php?lang=fr [L,QSA]

    # forgot-password
    RewriteRule en/forgot-password forgot-password.php?lang=en [L,QSA]
    RewriteRule fr/mot-de-pass-oublie forgot-password.php?lang=fr [L,QSA]



    # login
    RewriteRule en/login login.php?lang=en [L,QSA]
    RewriteRule fr/connexion login.php?lang=fr [L,QSA]

    # pricing
    RewriteRule en/pricing pricing.php?lang=en [L,QSA]
    RewriteRule fr/tarification pricing.php?lang=fr [L,QSA]

    # try
    RewriteRule en/try try.php?lang=en [L,QSA]
    RewriteRule fr/essayer try.php?lang=fr [L,QSA]

 # index
    RewriteRule en/ index.php?lang=en [L,QSA]
    RewriteRule fr/ index.php?lang=fr [L,QSA]