homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Mod Rewrite basics
sorry for yet another one

 12:12 pm on Nov 29, 2002 (gmt 0)

Hello All

I am trying to get to grips with the mod rewrite. I know this type of thing has been covered before but I am having trouble getting the most basic of rewrites to work.

I have created two pages (1.htm and 2.htm) with unique content.

I have the following in my htaccess

RewriteEngine on
RewriteBase /mydirectory/
RewriteRule ^1\.htm 2.htm [R]

Now when I call 1.htm it appends the whole physical path to the page and so results in an error



So it is sort of looking for the right file but I have a large spanner that I donít know how to remove. I have read a few tutorials on this but the light is not coming on. I cannot find any details about what the [R] and other parameters mean let alone where I am struggling.

Any hints gratefully received. If I can get this right how the hell am I going to make
?id=X into /x.htm :)



 4:41 pm on Nov 29, 2002 (gmt 0)

OK I have been doing a little more reading and something puzzles me further.

The basic principle is this
RewriteRule currentURL rewrittenURL

Now say you set the base to /

RewriteBase /
RewriteRule ^mydirectory/(.*).php mydirectory/mypage.php?id=$1

So if I have this right when there is an instance of the url with the? it is rewritten to the other URL.

So on the page the link will look like mydirectory/25.php but when it is clicked the server knows that is after mydirectory/mypage.php?id=25 which in turn is rewritten in the URL bar.

Is that correct or is it required spit the urls out differently.

Dazed and confused


 4:57 pm on Nov 29, 2002 (gmt 0)

I feel your pain, UK. All the pages of my first dynamic site failed to get crawled. The dynamic pages, that is. I had the variables passed at the end of the url like so:


I knew there was a problem when I saw Gbot only visiting static pages such as "home", "about", etc..

So I think I need to restructure the urls and use mod_rewrite. I was reading some articles and thinking about taking out the .html and just having:


Then use mod_rewrite to send all requests for /widget_catalog/ and /widget_info/ to their respective scripts.


 5:05 pm on Nov 29, 2002 (gmt 0)


If the directory you "log in" to using ftp is also the top directory of your site, then you don't need RewriteBase.

For mod_rewrite [httpd.apache.org] RewriteRules, all you have to do is provide a regular-expressions [etext.lib.virginia.edu] pattern to be matched and a target URL, followed by optional flags to control what kind of redirect you want (if any) and whether any more rewrites need to be processed.

RewriteRule ^file1\.html$ /file2.html [R=301,L]

This rewrites [yourdomain.com...] to [yourdomain.com...] using an external redirect (this updates the URL in the visitor's browser address bar) and then it quits - BEcause of the [L] flag, no more rewrites following this one will be processed for the current http request. This is normally what you want to do to keep things simple.

RewriteRule ^file1\.html$ /new/file2.html [L]

This is an internal - or silent - rewrite only. The effect is simply to serve [yourdomain.com...] whenever [yourdomain.com...] is requested.

If you omit the [L] flag, mod_rewrite will continue through your rewrite rulesets, and modify the URL further if another rule's conditions are satisified.

Translating a query string to a filename is not a big deal, but you should get your basic rewrite working first.

If you should need more help, please post the entire server filepath to the new filename. The best place to find this info is in your error log entry from your first try above. Actually, any 404 error in that log will do. It will look something like "/users/yourdomain.com/public/widgets/blue.html"



 11:42 am on Dec 2, 2002 (gmt 0)

Thanks very much Jim.

Just trying to get to grips with the regex link, most useful.

Thanks once again for the whole post.


 5:12 pm on Dec 2, 2002 (gmt 0)

Right I think I am moving in the right direction.

RewriteEngine on
RewriteRule ^dir\/4\.php$ /dir/page.php?id=4 [L]

So when I look for page /dir/4.php I get the dynamic stuff. I have seen it with a variable also

RewriteRule ^dir\/(.*)\.php$ /dir/page.php?id=$1 [L]

But this does not work for me.

In additon I think I need to have a rewrite before this on that converts any dynamic looking text:

page.php?id=x to x.php

Would this be using a ereg replace?



 5:33 pm on Dec 2, 2002 (gmt 0)


RewriteRule ^dir\/(.*)\.php$ /dir/page.php?id=$1 [L]

But this does not work for me.

I assume you meant that "RewriteRule ^dir\/4\.php$ /dir/page.php?id=4 [L]" worked, and that
"RewriteRule ^dir\/(.*)\.php$ /dir/page.php?id=$1 [L]" did not work.

But how did it not work? What happens? Do you get a 404 or 500 error entry in your error log file? (If so, please include it)

.htaccess stuff is fairly difficult to debug remotely, so please be a specific as you can.



 11:04 am on Dec 5, 2002 (gmt 0)


I have been out the office for the last few days hence my lack of reply.

Yes you are right. When I have a definite number (eg 4) it rewrites and renders fine (woohoo - first for me). But when I try to set the variable option so that any number.php renders the correct page (providing it exists) it seems to get stuck in an infinite loop of some sort. So I dont actually get an error.

I was under the impression that where the variable occurs in the URL it could be assigned to a $1 and then used in the real querystring.

If I remove the "." from the (.*) I get an internal server error.

[Thu Dec 05 11:00:22 2002] [alert] [client] c:/phpdev/www/.htaccess: RewriteRule: cannot compile regular expression '^dir\/(*)\.php$'

So I think that is wrong :)

Thanks once again.


 1:04 pm on Dec 5, 2002 (gmt 0)

After Mucho messing about. The following worked

RewriteRule ^dir\/([0-9]+)\.php$ /dir/page.php?id=$1 [L]

Which is great for me.

Now if I have this right I will need to rewrite the text on the page that contains all the links. Correct?

So I need a condition that is alomst opposite to the above that replaces all the text on the page that contains

page.php?id=$1 with


So that an instance of the link below is rewritten
<a href="page.php?id=x">Link Text</a>
<a href="x.php">Link Text</a>

Has the process of rewrting text/word got a specific name, I have looked all morning with no luck



 1:37 pm on Dec 5, 2002 (gmt 0)

Hello UK. This stuff is tricky and addicting, isn't it? Check out this page [fluidthoughts.com]. I noticed they used "(.+)" instead of "(.*)".

I also noticed this from the virginia site:

To make matters worse, the list of what needs to be escaped differs from tool to tool. Some tools, for example, consider the "+" quantifier to have its normal meaning (as a ordinary plus sign) until it is escaped. If you're having trouble with a regex (a sed routine that won't parse or a grep pattern that won't match even though you're certain the pattern exists), try playing around with the escapes. Or better yet, read the man page.

I think I've seen this used also:


I'm a newbie to this, but just trying to throw out some ideas. Good luck with it.


 3:02 pm on Dec 5, 2002 (gmt 0)


Your rule is better than the one I posted. Using "([0-9]+)" makes the rule much more specific, and also avoids problems; The ".*" I used is "greedy" - it will try to match as much as it can, possibly including the ".php" on the end. I've never had such a problem with it, but there's always a first time!

You should replace the links on your public pages - the ones you allow search engines to see - with the short-form links so that the SEs will spider them and record them.



 3:28 pm on Dec 5, 2002 (gmt 0)

Thanks Jim

You should replace the links on your public pages

This is my next quandy. Quite often I work by trial and error. I have infact got the rendering of the link ok. As I know the id=x I can stuff that variable into the link

<a href=\"$idnumber.php\">Text</a>

Is this a good method or should I be using some form apache rewrite like what I think is used here to change w.mw (had to put the dot in) to WebmasterWorld. I suppose another use of being able to rewrite specific words on a page would be to strip adult words and replace them with a less offensive. Can this be done with apache or is this a php thing with the ereg_replace?

I do appreciate your time spent answering my daft questions.



 3:56 pm on Dec 5, 2002 (gmt 0)


mod_rewrite can't rewrite the link text on the pages for you. It only "rewrites" URLs as they are being translated to file pathnames - just before they are served, in other words.

To do on-page text replacement, you'll have to use a script.


<edit>Yes, your link text with the ID substitution looks OK to me.</edit>


 4:00 pm on Dec 5, 2002 (gmt 0)

Thanks Jim

You have saved me hours searching for something that does not exist LOL


If I see you at pubcon or similar there is beer with your name on it.

For now you will have have a virtual one:
"the UK reaches into a 19inch monitor and passes Jim a cold one"

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved