网址:变换" /news.php ID = 1"?到" /新闻/ 1 /" [英] URL: transform "/news.php?id=1" to "/news/1/"

查看:263
本文介绍了网址:变换" /news.php ID = 1"?到" /新闻/ 1 /"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一些更清洁的URL为我的网站。我已经发现媒体链接大量的关于在网址删除文件扩展名的问题,并设法让该工作。我希望把它更进了一步,虽然通过将一个URL例如 的http:// www.site.com/news.php?id=1 http://www.site.com/news/1/ 。我无法找到一个答案,这个具体的问题,所以我问在这里。我怎么能实现URL的那样?

重写规则我媒体链接在我的htaccess的:

  RewriteEngine叙述上
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^(([A-ZA-Z0-9 \  - ] + /)* [A-ZA-Z0-9 \  - ] +)?$ $ 1.PHP
 

解决方案

有很多的问题,在那里,回答你的问题。有两件事情你必须做的:

  1. 重定向丑陋的URL,以花哨的网址,使用户看到花哨的网址
  2. 在内部重写URL看中的丑陋的url,这样服务器实际上可以执行它。

您可以在这里找到 mod_rewrite的的文档。该解决方案使用了 [结束] 标记,它只能来自Apache 2.3.9及以上。你可以在这里找到文档。在第一个规则的结尾丢弃查询字符串。

  #REDIRECT丑陋的URL看中网址
的RewriteCond%{QUERY_STRING} ID =([^&放大器;] *)
重写规则^新闻\ .PHP $新闻/%1 /? [R]

#Internally改写看中网址为可用的网址
重写规则^新闻/([^ /] *)/ $ news.php?ID = $ 1 [结束]
 

I'm trying to make some more cleaner URL's for my website. I have allready found lots of questions regarding the removal of file extensions in URL's and managed to get that working. I want to take it a step further though by transforming an URL like http://www.site.com/news.php?id=1 into http://www.site.com/news/1/. I couldn't find an answer to this specific question so I'm asking it here. How could I achieve URL's like that?

Rewrite rules I allready have in my htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php

解决方案

There are plenty of questions out there that answer your question. There are two things you have to do:

  1. Redirect the ugly url to the fancy url, so that the user sees the fancy url
  2. Internally rewrite the fancy url to the ugly url, so that the server can actually execute it.

You can find the documentation for mod_rewrite here. This solution uses the [END] flag which is only available from Apache 2.3.9 and up. You can find the documentation here. The trailing ? in the first rule discards the query string.

#Redirect ugly url to fancy url
RewriteCond %{QUERY_STRING} id=([^&]*)
RewriteRule ^news\.php$ news/%1/? [R]

#Internally rewrite fancy url to a usable url
RewriteRule ^news/([^/]*)/?$ news.php?id=$1 [END]

这篇关于网址:变换" /news.php ID = 1"?到" /新闻/ 1 /"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆