繁体   English   中英

.htaccess mod_rewrite创建干净的URL

[英].htaccess mod_rewrite to create clean URLs

我有一个数据驱动的站点,该站点传递信息以使用$ _GET参数确定下一页应显示的内容。

我希望URL看起来更好并且结构简单。

我一直在阅读有关mod_rewrite的信息,但到目前为止未能实现。

<?php $post = $_GET['ID']; ?>
<?php $loca = $_GET['loca']; ?>

这是从URL中提取的,以确定我们想要的表和帖子ID。 目前的网址是index.php?ID=4&loca=Pages

如果不是的话,我将如何进行这项工作。 /pages/(the name column of the post of this ID)

这应该进行内部重写:

RewriteEngine On
RewriteRule ^pages/(\d+)/ /index.php?ID=$1&loca=pages

它将任何以pages/(some number)/开头的URL重写为结果。 如果url并非完全是/pages/id/(Name that matches id)/可能还应该添加一些服务器端逻辑来进行302重定向。 您可以使用$_SERVER['REQUEST_URI']获取字符串,然后将其与应为字符串的字符串进行比较,如果不匹配,则进行重定向。

就像您访问以下https://stackoverflow.com/questions/11057691/This+is+not+the+title一样: https://stackoverflow.com/questions/11057691/This+is+not+the+titlehttps://stackoverflow.com/questions/11057691/This+is+not+the+title是+不是+ https://stackoverflow.com/questions/11057691/This+is+not+the+title

您将被重定向到具有正确标题的版本。 您还应该更新站点周围的链接,以使用新的URL格式。

很多关于如何在Google上执行此操作的示例。

教程: http//wettone.com/code/clean-urls

Mod_rewrite: http : //httpd.apache.org/docs/current/mod/mod_rewrite.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM