Forum Moderators: phranque
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
Eg
Ht*tp://localhost/c:/phpdev/www/mydirectory/2.htm
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 :)
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
/widget_catalog.html/red_widget/1/
/widget_catalog.html/blue_widget/2/
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:
/widget_catalog/1
/widget_catalog/2
/widget_info/1
/widget_info/2
Then use mod_rewrite to send all requests for /widget_catalog/ and /widget_info/ to their respective scripts.
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]
RewriteRule ^file1\.html$ /new/file2.html [L]
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"
Jim
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?
Cheers
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.
Jim
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 127.0.0.1] c:/phpdev/www/.htaccess: RewriteRule: cannot compile regular expression '^dir\/(*)\.php$'
So I think that is wrong :)
Thanks once again.
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
x.php
So that an instance of the link below is rewritten
<a href="page.php?id=x">Link Text</a>
to
<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
Cheers
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.
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.
Jim
You should replace the links on your public pages
eg
<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.
Cheers
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.
Jim
<edit>Yes, your link text with the ID substitution looks OK to me.</edit>