4

I'm trying to make my website display the other pages as a www.example.com/pageone/ link instead of www.example.com/pageone.html.

Problem is, i'm reading up on ways to do that using .htaccess and its getting me confused because i don't understand the commands.

I understand that the first step, however, is to write this in the .htaccess file:

RewriteEngine On

After this step, i have absolutely no idea whats !-d nor {REQUEST_FILENAME} nor ^(.*) and all that programming text. Is there a documentation that i can refer to?

Or can anyone provide me a simple explanation on how to configure the .htaccess file to understand that if i want to go to

www.example.com/pageone.html

, all i need to type into the URL is

www.example.com/pageone/

and PHP files, etc as well?

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
Kyle Yeo
  • 2,270
  • 5
  • 31
  • 53
  • 2
    ..and i AM using google, fyi -- i think its just that my keywords are a bit retarded. i can't seem to find any documentation on this – Kyle Yeo May 05 '12 at 08:39
  • 2
    The keyword you'd want to use is `mod_rewrite`: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html – AlienWebguy May 05 '12 at 08:43

3 Answers3

9

First of all, there's the Official Documentation. To solve your specific problem, I would go about this way:

RewriteEngine on #Turn on rewrite

RewriteCond %{REQUEST_FILENAME} !-f #If requested is not a filename...
RewriteCond %{REQUEST_FILENAME} !-d #And not a directory
RewriteRule ^([^/]+)/?$ /$1.html [L] #Preform this redirect

The RewriteConds only apply to the next following rule. If you were to have multiple rules, you'd need to write the conditions for each one.

Now, the Apache server matches the requested path (everything after www.example.com/), to see if it matches any of the rules you've specified. In which case, there is only one:

^([^/]+)$

This regular expression matches any number of characters, which are not slash /, followed by an optional trailing slash. If the match was found, it will rewrite the request to the second parameter: /$1.html, $1 means "Whatever was matched between the brackets", which in our case is all of the non-slash characters.

The [L] flag, tells the rewriting engine to stop looking for rules if this rule was matched.

So to conclude, www.example.com/whatever/ will be rewritten sliently at the server to www.example.com/whatever.html

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • Right, i think i'm going to start to learn a bit more about this mod_rewrite thingy. Thanks for the link and explanation! – Kyle Yeo May 05 '12 at 16:00
  • Hmm, can i do this for PHP as well? Or any other extensions for that matter? – Kyle Yeo May 05 '12 at 16:33
  • @RenoYeo: This is Apache stuff, it belongs to the server you are running. If you want PHP (i.e. the server-side language) to take control of things, you rewrite all the URLs to a single point of entry in PHP, and then control things from there. (redirect www.example.com/some/pages/ to www.example.com/index.php?route=some/pages/ and use PHP to control it). – Madara's Ghost May 05 '12 at 16:40
  • not in php, for php -- i meant for extensions in .php, .aspx, etc, i want them all to be reduced to www.example.com/examplefile/ instead of www.example.com/examplefile.aspx, etc – Kyle Yeo May 06 '12 at 04:03
  • Yes, it works that way. Just replace HTML with PHP or whatever. – Madara's Ghost May 06 '12 at 04:07
2
RewriteEngine on
RewriteBase /

RewriteRule ^([^/]+)$ /$1.html

That should be all you need for this rewrite. It basically says "Anything that is not a forward slash will be assigned to the variable $1. So /foo would point to /foo.html

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
1

For official documentation you can look here Apache httpd mod_rewrite.
On Google you can search with keywords such as url rewriting tutorial.
The weird characters are called regular expressions. It's not an easy part to learn but there is a lot of tutorial about them.
PS: this is not a straight answer but some stuff to let you go further and understand how url rewriting works.

grandouassou
  • 2,500
  • 3
  • 25
  • 60