Php 用文本重写URL

Php 用文本重写URL,php,mysql,.htaccess,Php,Mysql,.htaccess,您好,我正试图从news.php?id=1&title=世界需要您 IDK如何使用htacces,我正在使用mysql数据库存储信息并在页面上显示 这是我的.htaccess ErrorDocument 403 /errors/403.php ErrorDocument 404 /errors/404.php Options -Indexes Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteRule news/[0-

您好,我正试图从
news.php?id=1&title=世界需要您
IDK如何使用htacces,我正在使用mysql数据库存储信息并在页面上显示

这是我的.htaccess

ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php

Options -Indexes
Options +FollowSymLinks

RewriteEngine On
RewriteBase /
RewriteRule news/[0-9]+/[.*].html news.php?id=$1&tile=$2

你就是这样做的

Options +FollowSymLinks
RewriteEngine on
RewriteRule news/(.*)/(.*)/ news.php?id=$1&title=$2
请注意这两个
(.*)
,因为它们根据参数分别映射到$1和$2

重写的URL应如下所示:

http://localhost/news/1/The World Needs You/ 
编辑:

用连字符替换空格。最简单的方法是:

1)更新规则

RewriteRule news/(.*)/(.*) news.php?id=$1&title=$2
2)使用

/news/1/The-World-Needs-You
3)如果您计划在页面中显示标题。使用PHP将其替换回来

?php
     $title = $_GET['title'];
     $r = str_replace("-"," ",$title);
?
编辑2:

从评论中回答你的第三个问题


谢谢你的帮助。我还有一个问题。例如在这里 StachOverflow如果您稍微更改url,它将重定向到 正确的。你是如何做到的–BrxDgr8t 1小时前

我们首先需要查看堆栈溢出中的一个示例请求

http://stackoverflow.com/questions/41355986/url-rewrite-with-text/41356337?noredirect=1#comment69946939_41356337 
在这个url中,除了存在的所有额外信息外,它的关键是
/questions/
后面的id,在本例中是
41355986
。此编号是此问题的唯一标识符,如果他们使用某种类型的SQL数据库,则获取此问题的请求将是:

Select * from questions where id = 41355986
在此之后,您访问的标题或URL实际上并不重要,因为唯一标识符是ID。因此,只要ID
{41355986}
相同,以下任何示例都将指向此帖子

http://stackoverflow.com/questions/41355986/url-rewrite-with-text


http://stackoverflow.com/questions/41355986/new-fake-url
现在,如果您决定更改问题id中的任何数字,您将被重定向到一个新问题:

http://stackoverflow.com/questions/41355981/url-rewrite-with-text

如何用这样的催眠来替换空间谢谢你的帮助。我还有一个问题。例如,在StachOverflow中,如果稍微更改url,它将重定向到正确的url。你好it@BrxDgr8t Updated@BrxDgr8t不客气。请随意将问题标记为已回答