简体   繁体   中英

url rewrite rules with php and mysql

I am new in writing url rewrite rules. 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:

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

#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. For examples of how to do the latter, have a look at some of the posts by Anubhava ~ he is a master!

Add this code to htaccess file

 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

EDIT

Add this code to htaccess file

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

Your match page logic behind the scenes is most likely using 'mid' and 'sid' to do some kind of database lookup for the results.

Minimal change could be to change to a new url format like this:

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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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