【问题标题】:url rewrite rules with php and mysqlurl 用 php 和 mysql 重写规则
【发布时间】:2015-12-09 18:56:48
【问题描述】:

我是编写 url 重写规则的新手。你能帮我写重写规则吗?我当前的浏览器网址是

http://localhost/live/match-page.php?mid=770&sid=7

该网址的锚标记如下所示

<a target='_blank' href="match-page.php?mid=<?php echo $rec->ID?>&sid=<?php echo $rec->stream_id?>" style="color:#ffae00;">  <?php echo $rec->CatName; ?> - <?php echo $rec->Title; ?> </a>

我如何编辑 .htaccess file 然后显示我的浏览器网址,如下所示:

http://www.cricmelive.tv/live/177/south-africa-v-india-test-live-streaming
http://www.cricmelive.tv/live/Basketball-live-streaming

如何为 url 重写模式制作锚标签?

我确实对它进行了新的查询?并传递给数据库。如果可以的话怎么做。

【问题讨论】:

  • 你需要midsid
  • 是的,我两个都需要@AbhishekAgrawal

标签: php mysql .htaccess mod-rewrite url-rewriting


【解决方案1】:

我希望这些帮助。我有点困惑,你说你需要midsid 但是上面的例子是不同的~一个有midsid 参数而另一个只有一个,大概是sid

#htaccess

RewriteEngine On
RewriteBase /

RewriteRule ^live/(.+)$ live/match-page.php?sid=$1 [NC,L]
RewriteRule ^live/([0-9]+)/(.+)$ live/match-page.php?mid=$1&sid=$2 [NC,L]


/* php */
/* Match first style rewrite rule */
echo "<a target='_blank' href='/live/{$rec->stream_id}' style='color:#ffae00;'>  {$rec->CatName} - {$rec->Title} </a>";

/* match second style rewrite rule */
echo "<a target='_blank' href='/live/{$rec->ID}/{$rec->stream_id}' style='color:#ffae00;'>  {$rec->CatName} - {$rec->Title} </a>";

您可以通过两种方式使用urlrewrite - 一种如此处所示,您可以在其中生成链接以匹配重写规则中设置的模式,另一种方法是使用您最初拥有的链接并使用 apache 将丑陋的查询字符串变成新的、漂亮的 url。后者如何做的例子,看看Anubhava的一些帖子~他是高手!

【讨论】:

  • 让我试试。 @RamRaider
  • 如果原始 url basic links 一切正常,如在 print scr: (prntscr.com/9ceyxw) 中看到的那样,但在 url 重写代码中,我认为不要应用 css 文件为什么,请检查 print scr: (prntscr.com/9cf09d) 我的 url 重写代码是: RewriteRule ^([0-9]+)/([0-9]+)/(.+)$ match-page.php?mid= $1&sid=$2 [NC,L] 工作正常@RamRaider
【解决方案2】:

您要问的是如何用更简洁的方案(干净的 url)替换当前的 url 模式。

您在后台的匹配页面逻辑很可能使用“mid”和“sid”对结果进行某种数据库查找。

最小的更改可能是更改为新的 url 格式,如下所示:

http://www.example.com/live/MID/SID/text-descriptor
http://www.example.com/live/770/7/south-africa-v-india-test-live-streaming

.htaccess

RewriteEngine On
RewriteRule ^live/([0-9]+)/([0-9]+)/ match-page.php?mid=$1&sid=$2 [L]

然后更改您的链接以输出新模式:

$href = '/live/' . $rec->ID . '/' . $rec->stream_id . '/' . $rec->text; // text from category or title (isn't needed by script.)

【讨论】:

  • 如果原始 url 一切正常,如 print scr: [link](prntscr.com/9ceyxw) 但在 url 重写代码中,我认为不应用 css 文件为什么,请检查 print scr [链接](prntscr.com/9cf09d) @Progrock 为什么?感谢回复
  • @mc120400429 ,可能是因为您使用的是 CSS 的相对 URL 路径。因此,浏览器现在可能会尝试在 /live/123/456/ 或类似文件中查找 CSS(检查浏览器工具/网络/资产)。将您的 CSS 路径从站点的根目录更改为绝对路径。所以不要使用“css/site.css”,而是使用“/css/site.css”。
  • 感谢@Progrock 的回复,你猜对了,我从tools/network/css 检查它的状态码是正常的200 并且相对路径是错误的路径(localhost/live/330/4/templatemo_style.css)。我将它 templatemo_style.css 更改为 /templatemo_style.css 但它没有响应怎么办?我正在向您发送屏幕截图。 see in print screen shot
  • 鉴于上面的路径:/tools/network/css/templatemo_style.css.
  • 是吗?那条路径/tools/network/css/templatemo_style.css 是什么意思?我用浏览器中的 Inspect 元素检查它我把它当作但不起作用
【解决方案3】:

将此代码添加到 htaccess 文件中

 Options -MultiViews +FollowSymLinks
 RewriteEngine On
 RewriteRule ^/?live/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /match-page.php?mid=$1&sid=$2 [L]

在 match-page.php 上,你可以得到 midsid,假设你添加了像 http://www.cricmelive.tv/live/770/7/Basketball-live-streaming 这样的 url

编辑

将此代码添加到 htaccess 文件中

 Options -MultiViews +FollowSymLinks
 RewriteEngine On
 RewriteRule ^/?live/([0-9]+)/([0-9]+)/?$ /match-page.php?mid=$1&sid=$2 [L]

【讨论】:

  • 那怎么做anchor tage呢? @RamRaider 在 match-page.php 文件中为其创建链接。
  • @mc120400429 这个评论是给我的吗?
  • 哦,我能理解,让我试试@Abhishek Agrawal
  • 不适合 RamRaider 错误我在这里过去了 @Abhishek Agrawal
  • 我尝试使用您的两个代码,放入 htaccess 文件并使用 url 访问浏览器:localhost/live/770/7/Basketball-live-streaming。它不起作用@Abhishek Agrawal
猜你喜欢
  • 2016-03-15
  • 2018-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-07
  • 2014-11-07
相关资源
最近更新 更多