如何在php中创建友好的URL?

如何在php中创建友好的URL?

通常,显示某些配置文件页的做法或非常老的方法如下:

www.domain.com/profile.php?u=12345

哪里u=12345是用户ID。

最近几年,我发现了一些网址很好的网站,比如:

www.domain.com/profile/12345

如何在PHP中做到这一点?

就像一个疯狂的猜测,这是否与.htaccess档案?您能给我更多的提示或一些关于如何编写.htaccess档案?


慕少森
浏览 720回答 2
2回答

临摹微笑

根据这篇文章,您希望进行mod_rewrite(放置在.htaccess(文件)如下所示的规则:RewriteEngine&nbsp;onRewriteRule&nbsp;^/news/([0-9]+)\.html&nbsp;/news.php?news_id=$1这张地图是从/news.php?news_id=63到/news/63.html另一种可能是用forcetype,这会迫使任何东西沿着特定的路径使用php来评估内容。所以,在你的.htaccess文件中,放置以下内容:<Files&nbsp;news> &nbsp;&nbsp;&nbsp;&nbsp;ForceType&nbsp;application/x-httpd-php</Files>然后index.php可以根据$_SERVER['PATH_INFO']变量:<?php &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$_SERVER['PATH_INFO']; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;outputs&nbsp;'/63.html'?>

青春有我

最近,我在一个满足我需求的应用程序中使用了以下内容。.htaccess<IfModule&nbsp;mod_rewrite.c>#&nbsp;enable&nbsp;rewrite&nbsp;engine RewriteEngine&nbsp;On #&nbsp;if&nbsp;requested&nbsp;url&nbsp;does&nbsp;not&nbsp;exist&nbsp;pass&nbsp;it&nbsp;as&nbsp;path&nbsp;info&nbsp;to&nbsp;index.php RewriteRule&nbsp;^$&nbsp;index.php?/&nbsp;[QSA,L] RewriteCond&nbsp;%{REQUEST_FILENAME}&nbsp;!-f RewriteCond&nbsp;%{REQUEST_FILENAME}&nbsp;!-d RewriteRule&nbsp;(.*)&nbsp;index.php?/$1&nbsp;[QSA,L]</IfModule>index.phpforeach&nbsp;(explode&nbsp;("/",&nbsp;$_SERVER['REQUEST_URI'])&nbsp;as&nbsp;$part){ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Figure&nbsp;out&nbsp;what&nbsp;you&nbsp;want&nbsp;to&nbsp;do&nbsp;with&nbsp;the&nbsp;URL&nbsp;parts.}
打开App,查看更多内容
随时随地看视频慕课网APP