Jump to content
xisto Community
Sign in to follow this  
sparkx

Renaming Head With Php? Need help.

Recommended Posts

I see lots of websites that have .html files that contain php. I am little confused. How do you make it so a php file creates a .html file. I don't mean something like:

<html><?php//Php file...?></html>
But rather something that renames the file. I know how to do it to .png files. Is there a way to turn this php file into a .html file but the vars are: vars.html not index.html?vars. Get what I am saying? So the php file would look like:
index.php?ID=5

But the HTML would look like:
5.html
I see a lot of people doing this such as forums. I am not sure if this forum does it but when you look at the url it is: some_topic_t1111.html I was thinking there might be a way that uses a variation of:
header("(anti-spam-(anti-spam-content-type:)) image/png");
(well it doesn't work but replace anti-spam- with nothing)
Thank you everyone that helps out. I always woundered how websites do this and I was thinking I might use it unless it has some security holes or requires lots of bandwidth/space.
Thanks again,
Sparkx
Edited by sparkx (see edit history)

Share this post


Link to post
Share on other sites

It can be done with the use of the Apache Rewrite module (available at Xisto). When set up properly, it takes the entered URL and modifies it according to the rules you wrote. These rules must be contained in the .htaccess file, as that is where the server looks for them. With mod_rewrite you can do numerous things, and what you mentioned is just one.

 

There are many tutorials on the Internet, so Google is where you should start your search.

Share this post


Link to post
Share on other sites

It's almost identical to the png-php thing, but its html. There are some extra things, like {}-f, but I can't comment on them.

RewriteEngine onRewriteRule ^(*)\.html$ $1.php

Then theres stuff like [R] to redirect (ie, the user has any clue), or headers, different proccessor, etc.

Another way, in Cpanel I think theres a Mime-editor. Just make php parser .html.

Edit : btw, somehow I knocked myself off the .html stuff this forum uses, I think I prefer it.
Edited by toby (see edit history)

Share this post


Link to post
Share on other sites

Here's how the example you mentioned would be handled (5.html -> index.php?id=5)

 

RewriteEngine onRewriteRule ^(*)\.html$ index.php?id=$1

As you can see, quite similar to the example toby gave you, and here's an explanation of what is what. The first keyword we have is "RewriteRule" - it just tells the server that the following line contains a rewrite rule (obviously). Next, with ^ at the start and $ at the end is what we are "looking for". The star resembles any number of characters, until we get to a dot. The star is between parenthesis because we are storing it for later use, end the dot is escaped with a slash because it is a special character. "Html" is just there so we don't match other extensions.

 

After that goes what we are replacing the URL with. In our case, it is "index.php?id=", with $1 being a reference to the first thing in parenthesis (remember the "(*)"?). And that's all there is to it. Of course, this is just a really simple example, and the possibilities are much greater.

Share this post


Link to post
Share on other sites

Thank you very much. How would I have multiple vars though? Is there a way to have more then just one variable be put in the header. Example: index.php?ID=5&a=5 and display i5-a5.html just like this header. Or does it all need to be all one variable and I just combine everything in a string. Also there a simi-colon after each line right? So the actuall code would look like:

RewriteEngine on;RewriteRule ^(*)\.html$ index.php?id=$1;
Thank you for the help,
Sparkx

Share this post


Link to post
Share on other sites

... How would I have multiple vars though? Is there a way to have more then just one variable be put in the header. Example: index.php?ID=5&a=5 and display i5-a5.html just like this header. Or does it all need to be all one variable and I just combine everything in a string.

mod_rewrite can rewrite the entire URL and is more useful when you want to provide "friendly URLs".

 

Example 1:

.htaccessRewriteEngine OnRewriteRule ^/(.*)/post/(.*)/$ /index.php?language=$1&post=$2[L]

The above code will convert:

/es/post/10/ (easy to remember, cleaner, smarter)

to:

/index.php?language=es&post=10

 

You don't need to use ";" at the end of every line

 

Blessings!

Edited by develCuy (see edit history)

Share this post


Link to post
Share on other sites

As develCuy already pointed out, there are no semi-colons at the end of each line. It is somewhat like JavaScript, where you can split commands by entering a new line. In JS, however, you are allowed to use semi-colons, whereas in the .htaccess file I do not know if you can. Anyway, there is no need for that.

 

Example: index.php?ID=5&a=5 and display i5-a5.html just like this header

Just like this:

 

RewriteEngine onRewriteRule ^i([0-9]*)-a([0-9]*)\.html$ index.php?id=$1&a=$2

(I am not quite sure whether a dash is a special character, so it might need to be escaped with a slash)

 

The "[0-9]" tells the server that this character must be a digit. If you don't expect a piece of input to be a word (i.e. you want it in a specific form), it is always good to use this type of check.

Share this post


Link to post
Share on other sites

Thank you pyost, develcuy and toby. This works great for me. I ended up using the directory system. For the php project I am on it will work the best. I am trying out lots of things and it seems like re-naming the headers could be a real usefull tool (more then I expected). It is better then having a long url like index.php?a=1&b=... ect. People where saying my url was too long and hopefully this will change thier mind. I just had a few quick things that I was worried about.Not all vars required - Ok lets say I have a php file that is index.php?a=1&b=5. But the variable b is not required. If I wanted to have the url /1/5/. Is there a way to make it so you could type in /1/ without /1// or do I just need to make a new Rule?Vars after dot - I found this a little intresting that you could put vars after the dot example index.php&a=Test --> index.Test is this safe or do not all browsers support this? I just though that was a little intesting.Thank you very much for the help. I would never be able to learn php without your help. I guess it is a good thing I signed up for Xisto.Sparkx

Edited by sparkx (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.