url rewrite rules in htaccess with php mysql - php

How write url rewrite rules given below links.
original url for match pages:
http://www.cricmelive.tv/live/09876/barisal_bulls_vs_chittagong_vikings_t20
now needed url:
http://www.cricmelive.tv/live/09876/barisal_bulls_vs_chittagong_vikings_t20
2. original url for Channel Catgry:
http://www.cricmelive.tv/live/category.php?id=1
now needed url as:
http://www.cricmelive.tv/live/sports
3. original url for Sports Catgry:
http://www.cricmelive.tv/live/index.php?catid=1
now needed url as :
http://www.cricmelive.tv/live/cricket
4. orignal url for Channel Pages:
http://www.cricmelive.tv/live/channel-page.php?cid=45
now needed as :
http://www.cricmelive.tv/live/espn-3-live-streaming
I am new in ur rewrite. I post it again. Help me, how write rewrite url code again original url

I see what you are trying to do but it doesn't work that way. The parameters in the querystring are used at the backend in the sql to find the relevant records in the db using $_GET.
catid=45 does not directly translate to cricket - in the database perhaps catid=1 probably is cricket but the webserver doesn't know this relationship so you would need to alter your sql query such that it used cricket as the parameter in the where clause rather than 1.
For example:
http://www.cricmelive.tv/live/1
that could be expressed easily with a rewrite rule, such as:
RewriteRule ^live/([0-9]+)$ live/index.php?catid=$1 [NC,L]
select * from table where category='cricket'
RewriteRule ^live/([0-9a-zA-Z]+)$ live/index.php?category=$1 [NC,L]

Related

How to change change URL format to POST using htacess php?

I have htaccess tha handle redirections know i want to hit api like i have a url like that
http://localhost:8080/test/network/country/category/subcategory/fixedvalue
I have fixed value like detail, category ,codes but only in case of detail i need id as parameter
example
http://localhost:8080/test/Airtel/UK/Prepaid/5G/Detail
below is redirect url that i want to generate from htacess file
if fixed value is detail
http://localhost:8080/h1.php?network=Airtel&country=UK&category=Prepaid&subcategory=5G&Detail=remain portion of url except .html
in other cases where detail is not in fixed i don't need id that have parameter the remaining portion of url after 5th parameter so url will be
http://localhost:8080/h1.php?network=Airtel&country=UK&category=Prepaid&subcategory=5G
here is a code that redirect to 5.php file so that but i want not redirection i want a specific url having parameter
RewriteEngine On
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ 5.php?1=$1&2=$2&3=$3&4=$4&5=$5 [QSA,L]
in simple from
www.cloth.com/nike to www.cloth.com?name=nike

How to get Dynamic URL With help of .htaccess and PHP for Good SEO

For example i have
3 php pages
www.example.com/page1.php?var1=data1&var2=data2&var3=data3
www.example.com/page2.php?var1=data1&var2=data2&var3=data3
www.example.com/page3.php?var1=data1&var2=data2&var3=data3
For good SEO . I need URL like
www.example.com/page1/data1-data2-data3
www.example.com/page2/data1-data2-data3
www.example.com/page3/data1-data2-data3
I got something URL rewriting with PHP but am confused how to implement it for multiple dynamic PHP pages.
i need all the variables for proper functioning of php pages
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^(page1|page2|page3)/([^-]+)-([^-]+)-([^-/]+)/?$ /$1.php?var1=$1&var2=$2&var3=$3 [L,QSA,NE]
In your .htaccess you should add the line:
RewriteRule ^(\w+)/(\w+)-(\w+)-(\w+)$ /$1.php?var1=$2&var2=$3&var3=$4 [QSA]
The first part captures the page name (script name, in your case), then each successive query string parameter. The QSA key tells the redirect to take any additional query string parameters not specified explicitly along with the request.

How do I make clean url?

I want to make the clean url for my site. How do I change htaccess file. My original Link that
This is my original link
**http://www.tangailbazar.com/adview_details.php?ID=9014&show=Hot%20and%20Cool%20Water%20Filter**
I want to make it like this
http://www.tangailbazar.com/Hot-and-Cool-Water-Filter
My code is here.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /^(.*) adview_details.php?show=$1 [PT,QSA]
My post link from where i get the value like this
View Details
In adview_details page I have written the code
<?php
$id=$_GET['ID'];
$id1=$_GET['show'];
?>
<?php
$SQL="select * from tb_classified where sl='$id' and title='$id1'";
$obj->sql($SQL);
while($row = mysql_fetch_array($obj->result))
{
echo $row['title'];
echo $row['description'];
}
?>
You are looking for an Apache mod called mod_rewrite. Specifically (from the documentation):
If, on the other hand, you wish to pass the requested URI as a query string argument to index.php, you can replace that RewriteRule with:
RewriteRule (.*) adview_details.php?show=$1 [PT,QSA]
Note that these rulesets can be used in a .htaccess file, as well as in a block.
<?php
$title = str_replace(' ', '-', $row['title'])
?>
View Details
Output URL http://www.tangailbazar.com/Hot-and-Cool-Water-Filter
but this will not work, why? because you can not make a url with a condition, while for the request to mysql need two conditions.
maybe you should change your url into http://www.tangailbazar.com/1/Hot-and-Cool-Water-Filter
If you follow my advice you should change the code. htaccess becomes
RewriteRule ([0-9]?)/([a-zA-Z_-]+) adview_details.php?ID=$1&show=$2 [PT,QSA]
and the URL that you should use
View Details
First what draws my attention is that your regular URL is not as clean to start with. I would advise you to remove the spaces and change them for hyphens—if possible. URL are also case insensitive so there is actually no use for uppercase characters, but using them is personal preference though it makes them less readable and when rewriting it's an extra process to change them.
When rewriting a URL, you should think of your URL as parts. There is the domain which is divided in: subdomain (www), domain-name (tangailbazar) and the top-level domain (com). After the domain begins the cleaning up (you can also do a lot of rewriting on the domain, but in your case you will not).
To keep your rewrite process clear and simple (because URL rewriting with mod_rewrite can give massive headaches) you want every part of a rewritten URL (between slashes) to translate to a parameter in the raw URL. So in your case: ID=9014&show=Hot and Cool Water Filter are two parameters that should be used in your clean URL. When rewriting the URL you would pick the value of key (parameter) ID and show and use them in your clean URL. In your case you need the ID parameter in the clean URL otherwise you can never not load your destination URL.
RewriteRule ^.+/(.+)/(.+)+$ adview_details.php?ID=$1&show=$2 [L]
This rule will change the request for:
www.tangailbazar.com/9014/Hot%20and%20Cool%20Water%20Filter
into:
www.tangailbazar.com/adview_details.php?ID=9014&show=Hot%20and%20Cool%20Water%20Filter
This is the best you can do in this case, otherwise you will have to change some things about the structure of the destination URL.
An important thing you need to get hold of is that the name URL rewriting is misleading, you actually don't rewrite anything, you translate the requested URL from the browser into the URL where you're page is located. So it's more of a 'URL translate' then a 'URL rewrite'. In creating user friendly URL's you most of the time are removing the parameter keys (show=) and file extension (.php) from the URL to make them more readable.
You will always need to use the dynamic parts in your URL and you can clean the static parts.
I hope this will help you solve your problem or at least make some things clear on rewriting URL's.
Good luck!
httpd.apache.org/docs/current/mod/mod_rewrite.html
www.regexr.com

Mod Rewrite faux-directory path with extra query string

I'm trying to write a rewrite rule in my .htaccess file that has the following behavior. I'm doing it for an API which is located at a real location in the form of:
https://www.domain.com/apifolder/entry.php?call=testcall&data=testdata
The API call is routed starting from entry.php and eventually delivers the appropriate response. Essentially my entire API lands upon a large switch statement of valid API calls. The problem is, that URL is not very nice. I'm trying to replace it with a URL of this form:
https://www.domain.com/api/testcall?data=testdata
I have gotten close with the following rewrite rule:
RewriteRule ^api/([^/]+)/? /apifolder/entry.php?call=$1 [L]
However, the desired example above does not work, because there first parameter that seems to coming in (data, which is in reality the second parameter), is led by a ?. If I try the url:
https://www.domain.com/api/testcall&data=testdata
where it's led by a & instead, then it works perfectly fine. This looks weird to me too, sadly. Is there anything I can do to let the rewritten URL take in the query string beginning with a ? instead?
I managed to solve this by including the [QSA] flag in the rule, which passes the query string (in its desirable format) to the real URL:
RewriteRule ^api/([^/]+)/? /apifolder/entry.php?call=$1 [QSA,L]
I tried it after seeing this StackOverflow answer: Match Question Mark in mod_rewrite rule regex

Is it possible to use mod_rewrite htaccess to rewrite a url from name to id.

If I have a URLhttp://www.domain.com/listing.php?company_id=1 is it possible for me to re-write that to http://www.domain.com/company-name by using that id to pull the name from the database.
Or do I have to change listing.php to make it ?company_name=company-name
If anyone can point me in the right direction that would be great.
Thanks!
A map function allows using no id in the url at all, while still using the id in the rewritten url to do the database lookup.
http://www.domain.com/another-one
maps to
http://www.domain.com/listing.php?company_id=3423
Here is how I use map text files, which I generate from a database query. I believe mod_rewrite also does mapping to a database directly, of which I'm unfamiliar (maybe someone can provide that answer). I use Helicon Tech's isapi_rewrite v3, which works like mod_rewrite, so this should work for you.
Sample map file named map_company.txt
some-company 12
another-one 3423
freds-fill-dirt-and-croissants 44
The rewrite rules:
RewriteMap map_company txt:map_company.txt [NC]
RewriteCond ${map_company:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^/(.*) /listing.php?company_id=${map_company:$1} [NC,QSA,L]
RewriteMap assigns the map_company.txt text file to a variable named map_company (named the same just to be consistent).
RewriteRule is doing the work. It captures everything after the slash into $1, then rewrites it to your listing.php url. The ${map_company:$1} is looking up the url in the map file, and returning the id.
RewriteCond is just doing a double-check to see if the listing is there. The $1 is coming from the RewriteRule url, the NOT_FOUND is a default value if not found, and the condition is if it's not-NOT_FOUND (a little tangled - but it's just checking if it's in the file). If it's in the file, the RewriteRule will be run. If it's not in the file, it skips the RewriteRule, and falls through to more rules (perhaps to a page-not-found or some other default).
You'll need the id in both the urls, but only use the full name in the pretty link for the user.
The following would rewrite http://www.domain.com/42/company_name to http://www.domain.com/listing.php?company_id=42 and just discard the company name.
RewriteEngine on
RewriteRule ^([0-9]*)/([A-Za-z0-9_]*)/$ /listing.php?company_id=$1 [L]
Note that a user could also visit http://www.domain.com/42/wrong_name and still land on the right page with the right company name. If this isn't desired you could change the rule to /listing.php?company_id=$1&company_name=$2 and check for equality in listings.php

Categories