php URL - replace question mark and parameter with slash

ⅰ亾dé卋堺 提交于 2020-01-15 03:40:58

问题


So the orginal url looked like

website.com/post.php?id=130

using the following htaccess rules i was able to remove .php from the url

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

now the url looks like this

 website.com/post?id=130

Now i wish to replace the "?id=" with a slash to make the url look like

website.com/post/130

any tips on what to do here ?


回答1:


You need an additional rule for that:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteCond %{DOCUMENT_ROOT}/proj1/$1.php -f
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?id=$2 [L,QSA]

RewriteCond %{DOCUMENT_ROOT}/proj1/$1.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]


来源:https://stackoverflow.com/questions/41439512/php-url-replace-question-mark-and-parameter-with-slash

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