Execute .php to .html

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
loveme
Forum Newbie
Posts: 5
Joined: Mon May 12, 2014 11:04 pm

Execute .php to .html

Post by loveme »

Hi... I want php file shown in browser as .HTML or like folder. Here I explain:
I have php code in (index.php) as <a href="cat.php?Id=$id"> something like this.Now When user click the link it show in browser as it be,like ...cat.php?Id=3 . But I want here , when user go to(by click) cat.php?id=$id the browser show the URL as ...cat/3.HTML or ...cat/3

Cat = SQL column name
Id= SQL 'cat' fields.
How do I do this...
Thanks in advance...
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Execute .php to .html

Post by requinix »

You can't show the user a different URL than the one they went to.

The subject is called "URL rewriting". You instruct Apache to rewrite "/cat/3.html" (what the user sees) to "cat.php?id=3" (what your PHP code actually needs).
Learn a bit about it before you continue, and then the following won't look so cryptic:

Code: Select all

RewriteEngine on
# Turn /cat/#.html to cat.php?id=#
RewriteRule ^/?cat/(\d+)\.html$ cat.php?id=$1 [L,QSA]
loveme
Forum Newbie
Posts: 5
Joined: Mon May 12, 2014 11:04 pm

Re: Execute .php to .html

Post by loveme »

Hi... Thank you. I am learning this... I can achieve this type now. What should I do if the same think as cat.php?Id=3&pg=2
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Execute .php to .html

Post by requinix »

Link to /cat/3.html?pg=2.
loveme
Forum Newbie
Posts: 5
Joined: Mon May 12, 2014 11:04 pm

Re: Execute .php to .html

Post by loveme »

Hi...I just improved ( I think ) ... Here I removed .HTML now it looks as local host/3 for local host/cat.php?Id=2 with htaccess Rewrite rule ^/?(\d+) $ cat.php?Id=$1 [L,QSA] . Now I want to show cat.php?Id=3&pg=5 to local host/3/5
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Execute .php to .html

Post by Celauran »

/cat/3/page/5 is a far more meaningful URI than /3/5

That said

Code: Select all

^/?(\d+)/(\d+)/? cat.php?id=$1&pg=$2
You may be interested in this htaccess tester
loveme
Forum Newbie
Posts: 5
Joined: Mon May 12, 2014 11:04 pm

Re: Execute .php to .html

Post by loveme »

Works fine. But next page URL take the previous no.
Ex. local host/cat/3/page/1 when I click 2 page it show the URL as local host/cat/3/pg/cat/3/pg/2 how can I rectify this
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Execute .php to .html

Post by Celauran »

Sounds like you're using relative instead of absolute links. Try adding a leading slash.
loveme
Forum Newbie
Posts: 5
Joined: Mon May 12, 2014 11:04 pm

Re: Execute .php to .html

Post by loveme »

Oh...thanks... Can we use it with relative links ...
Post Reply