简体   繁体   English

用php和mysql重写URL规则

[英]url rewrite rules with php and mysql

I am new in writing url rewrite rules. 我是写URL重写规则的新手。 Can you help me in writing rewrite rules. 您能帮我编写重写规则吗? My current browser url is 我当前的浏览器网址是

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

My anchor tag for that url is given below 我在下面给出了该网址的锚标记

<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>

How I edit .htaccess file then I show my browser url like given bellow here: 如何编辑.htaccess file然后在下面显示我的浏览器网址,如下所示:

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

how make anchor tage for url rewrite mode? 如何为网址重写模式设置锚点数?

It is true that I made new queries for it? 的确,我对此进行了新查询? And pass to the database. 并传递给数据库。 If it is possible how do it. 如果有可能怎么做。

I hope these help. 希望这些帮助。 I'm a little confused though, you say you need both mid & sid yet the examples above are different ~ one has both the mid and sid parameters yet the other only has one, presumably sid 不过,我有点困惑,您说您需要同时使用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>";

There are two ways in which you can use urlrewrite - one as shown here where you generate your links as shown to match the pattern set in the rewrite rule and the other method is to use the links as you had them originally and use apache to make the ugly querystring into the new, pretty url. 可以使用两种方式使用urlrewrite -一种是如图所示的方式,您可以生成所示的链接以匹配重写规则中设置的模式,另一种方法是使用与原始链接相同的链接并使用apache进行链接将丑陋的查询字符串插入新的漂亮网址中。 For examples of how to do the latter, have a look at some of the posts by Anubhava ~ he is a master! 有关后者的示例,请查看Anubhava的一些帖子〜他是大师!

Add this code to htaccess file 将此代码添加到htaccess文件

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

On match-page.php, you can get mid and sid , assuming you add url like http://www.cricmelive.tv/live/770/7/Basketball-live-streaming 在match-page.php上,假设您添加了http://www.cricmelive.tv/live/770/7/Basketball-live-streaming类的http://www.cricmelive.tv/live/770/7/Basketball-live-streaming ,则可以获取midsid http://www.cricmelive.tv/live/770/7/Basketball-live-streaming

EDIT 编辑

Add this code to htaccess file 将此代码添加到htaccess文件

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

What you are asking is how can you replace your current url patterns with a cleaner scheme (clean urls). 您要问的是如何用更干净的方案(干净的URL)替换当前的URL模式。

Your match page logic behind the scenes is most likely using 'mid' and 'sid' to do some kind of database lookup for the results. 后台的匹配页面逻辑很可能使用“ mid”和“ sid”对结果进行某种数据库查找。

Minimal change could be to change to a new url format like this: 最小的更改可能是更改为新的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 .htaccess

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

Then change your links to output the new pattern: 然后更改您的链接以输出新模式:

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

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

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