A Quick Tip on Mod Rewrite

  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Posts: 4581
  • Loc: Maryland

Post 3+ Months Ago

Hey everyone,

I just discovered the wonders of Mod Rewrite (and I actually think I have it down pat). Anyways, it is something this forum implements "masking" the messy viewforum.php?foo=bar&foo2=bar2.

It's actually quite simple, all that you need is an apache based http server, and something to type in, notepad for windows, kate for linux.

Start up your editor and put these lines in there, don't worry I'll explain later :)
Code: [ Select ]

RewriteEngine On
RewriteRule ^index(.*).php$ index.php?foo=$1 [L,NC]
  1. RewriteEngine On
  2. RewriteRule ^index(.*).php$ index.php?foo=$1 [L,NC]


Ok to start off,
Code: [ Select ]

RewriteEngine On
  1. RewriteEngine On

Just tells apache you will be "masking" some filenames.

The next line:
Code: [ Select ]

RewriteRule ^index(.*).php$ index.php?foo=$1 [L,NC]
  1. RewriteRule ^index(.*).php$ index.php?foo=$1 [L,NC]


Tells apache you are rewriting anything that is in the format indexbar.php to index.php?foo=bar, and that it is all being done on the local server.

Save this file as .htaccess. If you are in windows, save it as htaccess.txt then upload it, and use your ftp client's rename function to rename it to .htaccess.

So now whenever you want to pass variable "foo" with value "bar" within your page simply link to indexfoo.php.

Other forms can be created, allowing multiple variables such as:
Code: [ Select ]

RewriteRule ^index_(.*)_(.*)_(.*).php$ index.php?foo=$1&foo2=$2&foo3=$3 [L,NC]
  1. RewriteRule ^index_(.*)_(.*)_(.*).php$ index.php?foo=$1&foo2=$2&foo3=$3 [L,NC]


That just tells apache that index_bar_bar2_bar3.php really is index.php?foo=bar&foo2=bar2&foo3=bar3.

The reason for all of this is that it is easier for google to index your pages, and MORE pages will get indexed because index.php?foo=bar and index.php?foo2=bar2&foo3=bar3 are similar and don't count as much to your innersite links (correct me if I am wrong). But indexbar.php and index_bar2_bar3.php are two dissimilar links and are treated as thus.

Hope you all like this! It works great for me :)
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • hgruber
  • Beginner
  • Beginner
  • User avatar
  • Posts: 58
  • Loc: Vero Beach Florida

Post 3+ Months Ago

Not sure I really understand all of that.

But, all you're doing is renaming index.html to index.php files withing the www/root, or within a specific directory?
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

Nice little article B_heyer, thanks for that - I've been looking for a tutorial that is explained in simple terms for a while. As I have no need for rewrite at the moment, I can never be bothered to learn from more complicated sources!

BTW, if you want .htaccess on windows, upload htaccess.txt, rename to .htaccess then ftp it back on to your harddrive. I have no idea why windows accepts that, but the quirky thing does :lol:
  • basdog22
  • Novice
  • Novice
  • User avatar
  • Posts: 21
  • Loc: Hellas

Post 3+ Months Ago

I also tried it and it works fine

i did :

/articles/(a-z0-9)* index.php?page=articles/$1

and it worked great. But i never got the right paths for images .

When i needed an image from /images/foo.gif
using rewrite it looked at /articles/$1/images/foo.gif

some other problems (small) arise too :roll:
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

b_heyer wrote:
If you are in windows, save it as htaccess.txt then upload it, and use your ftp client's rename function to rename it to .htaccess.



Added tip for Windows users. Something many don't know about Notepad: By default, Notepad adds the .txt as a file extension as b_heyer seemed to indicate. You can skip this by choosing Save As. Use the drop down menu to Save As "All Files"; Save your document as .htaccess as originally indicated and it will be saved without the .txt extension.

This is also a very handy trick when working with .tpl files or .inc , etc., to avoid the same problem of the automatically added .txt extension.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

ATNO/TW wrote:
b_heyer wrote:
If you are in windows, save it as htaccess.txt then upload it, and use your ftp client's rename function to rename it to .htaccess.



Added tip for Windows users. Something many don't know about Notepad: By default, Notepad adds the .txt as a file extension as b_heyer seemed to indicate. You can skip this by choosing Save As. Use the drop down menu to Save As "All Files"; Save your document as .htaccess as originally indicated and it will be saved without the .txt extension.

This is also a very handy trick when working with .tpl files or .inc , etc., to avoid the same problem of the automatically added .txt extension.


Don't know about any others but win2k I just Ctrl+S (brings up the save-as screen anyway if its a new document) type in the name WITH the extension & save. Even though the save as type says *.txt it still gives me what I want :D
  • DavyDuke17
  • Graduate
  • Graduate
  • DavyDuke17
  • Posts: 166

Post 3+ Months Ago

Sorry for the digging, but I tried using b_heyers thing for my forum, and I cannot get it to work correctly. I want it to make viewforum.php?f=1 to viewforum1.php . Here is what I tried:
Code: [ Select ]
RewriteEngine On
RewriteRule ^forum/viewforum(.*).php$ forum/viewforum.php?f=$1 [L,NC]
  1. RewriteEngine On
  2. RewriteRule ^forum/viewforum(.*).php$ forum/viewforum.php?f=$1 [L,NC]

When I do that it says the forum you selected does not exist...what did I do wrong?
  • DavyDuke17
  • Graduate
  • Graduate
  • DavyDuke17
  • Posts: 166

Post 3+ Months Ago

wth, I guess the htaccess on ozzu changed what I wanted when I said
Quote:
I want it to make forum1.html to viewforum1.php
What I really wanted to say is and take out the spaces: I want to make viewforum . php? f=1 to viewforum1.php

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 144 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.