为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕少森 2019-06-05 13:26:25
如何在php中创建友好的URL?通常,显示某些配置文件页的做法或非常老的方法如下:www.domain.com/profile.php?u=12345哪里u=12345是用户ID。最近几年,我发现了一些网址很好的网站,比如:www.domain.com/profile/12345如何在PHP中做到这一点?就像一个疯狂的猜测,这是否与.htaccess档案?您能给我更多的提示或一些关于如何编写.htaccess档案?
查看完整描述

2 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞

根据这篇文章,您希望进行mod_rewrite(放置在.htaccess(文件)如下所示的规则:

RewriteEngine onRewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1

这张地图是从

/news.php?news_id=63

/news/63.html

另一种可能是用forcetype,这会迫使任何东西沿着特定的路径使用php来评估内容。所以,在你的.htaccess文件中,放置以下内容:

<Files news>
    ForceType application/x-httpd-php</Files>

然后index.php可以根据$_SERVER['PATH_INFO']变量:

<?php
    echo $_SERVER['PATH_INFO'];
    // outputs '/63.html'?>


查看完整回答
反对 回复 2019-06-05
?
青春有我

TA贡献1784条经验 获得超8个赞

最近,我在一个满足我需求的应用程序中使用了以下内容。

.htaccess

<IfModule mod_rewrite.c># enable rewrite engine
RewriteEngine On

# if requested url does not exist pass it as path info to index.php
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]</IfModule>

index.php

foreach (explode ("/", $_SERVER['REQUEST_URI']) as $part){
    // Figure out what you want to do with the URL parts.}


查看完整回答
反对 回复 2019-06-05
  • 2 回答
  • 0 关注
  • 719 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信