【问题标题】:URL Rewriting with multiple parameters without changing URL在不更改 URL 的情况下使用多个参数重写 URL
【发布时间】:2014-12-29 10:57:29
【问题描述】:

我想重写我的网址

发件人:

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

收件人:

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

当用户访问此 URL 时:

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

应该是这样的:

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

如果用户访问此 URL:

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 .但是当我在 URL 中获得 token 时,它就不起作用了。

对于这种情况,正确的 .htaccess 代码是什么?

【问题讨论】:

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


    【解决方案1】:

    您需要为新参数制定一套新规则:

    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]
    

    【讨论】:

    • 很好的答案,终于我明白了,因为你的措辞内部和外部:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 2012-05-22
    • 2018-10-27
    • 1970-01-01
    • 2013-08-11
    • 2013-11-16
    相关资源
    最近更新 更多