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

时光怂恿深爱的人放手 提交于 2019-12-02 17:28:18

问题


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 write my htaccess file to produce these results?

Dynamic URL:

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

URL I want:

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

回答1:


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]



回答2:


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.



来源:https://stackoverflow.com/questions/30012589/rewrite-mysite-com-app-phpappname-example-nameappid-numeric-id-to-mysite-com-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!