Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have a php template which has one index.php and all the different pages (content1.html, content2.html, etc) are shown on the index.phppage. So for example I can open www.example.com/index.php?content1 and it will show the content1.html.

Now what I would like is this: often you see websites where the URL is like: www.example.com/this-is-the-content. I know how to do this with an exclamation mark like www.example.com/?content1 where you just don't write the index.php.

But how can I make a URL name, which is completely different from the filename? For example, www.example.com/this-is-some-page would show me the content of index.php?content1

share|improve this question

migrated from stackoverflow.com Jun 29 '11 at 18:12

This question came from our site for professional and enthusiast programmers.

3 Answers 3

.htaccess is definitely one route you could go for rewriting URL's. But, I think that the piece you're missing is the part where you should store your page's content in a database or store the file names and URL slugs of the pages in an array on index.php to pull from. This way you can have a different URL slug than the actual file name.

For instance, if you use a rewrite rule in .htaccess that says all requests for the URL http://www.example.com/the-page-you-want need to display the content from content1.html, you will need to specify somewhere that 'the-page-you-want' refers to content1.html.

share|improve this answer

refer to @Pål Brattberg's answer for detailed description

@phpheini You don't need to bind the .htaccess file to the database, suppose if you want the URL for your article like

http://example.com/sports/45/world-cup-2011.html

RewriteEngine On
RewriteBase /
RewriteRule ^([a-z]+)/([0-9]+)/(.*?).html$ /index.php?type=$1&article_id=$2

Here $1 and $2 get the article type and article_id which can change. index.php can output content based on the parameters.

share|improve this answer

You probably should look into mod_rewrite which could help you out.

See this cheat sheet for some help.

share|improve this answer
    
ok thanks to you 2, this would work for static pages. But what about page names which are dynamically created and stored in a mysql database. Say for example I create a new page index.php?newpage and I save the url name of the page in a database. Now the htaccess would have to connect to the database to get the url name wouldnt it? Btw stackoverflow is doing exactly what I want to achieve, look at the url stackoverflow.com/questions/6524908/… –  phpheini Jun 29 '11 at 17:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.