Rewrite mysite.com/app.php?appname=example-name&appid=numeric-id to mysite.com/app/app-name/numeric-id/

后端 未结 2 1134
隐瞒了意图╮
隐瞒了意图╮ 2021-01-26 11:01

I am new to PHP and server-side operations so asking this basic question here. I found a lot of similar questions and answers here but I fail to achieve what I want. How can I w

相关标签:
2条回答
  • 2021-01-26 11:45

    To rewrite

    http://mysite.com/app.php?appname=example-name&appid=numeric-id

    to

    http://mysite.com/app/example-name/numeric-id

    you can use these rules:

    RewriteEngine on
    RewriteCond %{QUERY_STRING} appname=([^?&]+)&appid=([^?&]+) [OR,NC]
    RewriteCond %{QUERY_STRING} appid=([^?&]+)&appname=([^?&]+) [NC]
    RewriteRule ^app\.php.*$ /app/%1/%2? [R=301,L]
    

    These rules capture the query string parameters you want, then use them in the rewrite in the order they were captured. There are two conditions in case the order of the parameters are switched. The trailing question mark (?) removes the original query string from the rewritten output.

    301 Redirect Tip:

    If your past .htaccess rules contain a 301 redirects (301 = permanent), even if it is incorrect, your browser will cache that redirect and not consult the .htaccess anymore for that URL.

    For testing, use 302 redirects, then when you are satisfied switch to 301. If it's too late, then try adding another parameter to your request to temporarily bypass this, like:

    app.php?appname=example-name&appid=numeric-id &nonce=1234

    or, clear your browser cache, or use an ingocnito window.

    0 讨论(0)
  • 2021-01-26 12:02

    Try this in your .htaccess file

    RewriteEngine On
    RewriteRule ^app/(.*)/(.*)/?$ /app.php?appname=$1&appid=$2 [QSA,NC,L]
    

    EDIT :

     RewriteEngine On
     RewriteCond %{THE_REQUEST} app\.php\?appname=(.*?)&appid=([0-9]+) [NC]
    RewriteRule ^ /app/%1/%2? [R=302,L]
    
    RewriteRule ^app/(.*)/([0-9]+)/?$ /app.php?appname=$1&appid=$2 [L,QSA]
    
    0 讨论(0)
提交回复
热议问题