使用多个参数重写URL而不更改URL

时间:2014-12-29 10:57:30

标签: php .htaccess mod-rewrite url-rewriting

我想重写我的网址

自:

https://example.com/fr/transporter/transporterPublicProfile.php?profil=1927&token=xnbjfgh4534534534dgfsdsd4

要:

https://example.com/fr/profil-des-transporteurs/1927/xnbjfgh4534534534dgfsdsd4

当用户访问此网址时:

https://example.com/fr/transporter/transporterPublicProfile.php?profil=1927&token=xnbjfgh4534534534dgfsdsd4

应该是这样的:

https://example.com/fr/profil-des-transporteurs/1927/xnbjfgh4534534534dgfsdsd4

如果用户访问此网址:

https://example.com/fr/profil-des-transporteurs/1927/xnbjfgh4534534534dgfsdsd4

它应该保持不变。

到目前为止我尝试的是:

    Options +FollowSymLinks -MultiViews
    RewriteEngine On

    RewriteBase /fr/
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} /fr/transporter/transporterPublicProfile\.php\?profil=([^\s&]+) [NC]
    RewriteRule ^ fr/profil-des-transporteurs/%1? [R=302,L,NE]

    # internal forward from pretty URL to actual one
    RewriteRule ^profil-des-transporteurs/([^/.]+)/?(.*)$ transporter/transporterPublicProfile.php?profil=$1&token=$2 [L,QSA,NC]


    RewriteBase /

    #RewriteRule    ^/fr/shipper/(.*)$ https://example.com/fr/$1 [L,R=301]

    #RewriteRule    ^login.php https://example.com/fr/shipper/login.php [L]
    RewriteRule ^index\.html /index\.php......................

上面.htaccess中的问题是它可以正常使用一个参数,即 profil 。但是当我在网址中获得令牌时,它就无法正常工作。

此方案的正确.htaccess代码是什么?

1 个答案:

答案 0 :(得分:3)

您需要为新参数设置一组新规则:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /fr/

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /fr/transporter/transporterPublicProfile\.php\?profil=([^\s&]+)&token=([^\s&]+) [NC]
RewriteRule ^ profil-des-transporteurs/%1/%2? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /fr/transporter/transporterPublicProfile\.php\?profil=([^\s&]+) [NC]
RewriteRule ^ profil-des-transporteurs/%1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^profil-des-transporteurs/([^/.]+)/([^/]+)/?$ transporter/transporterPublicProfile.php?profil=$1&token=$2 [L,QSA,NC]

RewriteRule ^profil-des-transporteurs/([^/.]+)/?$ transporter/transporterPublicProfile.php?profil=$1 [L,QSA,NC]

RewriteRule ^index\.html /index\.php [L]