Please help

Discuss practical ways rearrange URLs using mod_rewrite.

Please help

Postby Erikina » Fri Apr 01, 2005 6:12 pm

I am very new to mod_rewrite, and read some basic stuff about it for the porupose of being able to do this. Unfortunately it doesn't work. (I have taken the http://www. from the URL's so they don't appear as links).

I want to rewrite a URL from fantastictutorials.com/php.html to fantastictutorials.com/new.php?tut=php

OR

fantastictutorials.com/php/introduction.html to fantastictutorials.com/new.php?tut=php&main=introduction

OR

fantastictutorials.com/CSS/basics/syntax.html to fantastictutorials.com/new.php?tut=CSS&main=basics&sub=syntax

This is what I've come up with:

Code: Select all
RewriteRule (.*).html   http://www.fantastictutorials.com/new.php?tut=$1
RewriteRule (.*)/(.*).html   http://www.fantastictutorials.com/catch.php?tut=$1&main=$2
RewriteRule (.*)/(.*)/(.*).html   http://www.fantastictutorials.com/catch.php?tut=$1&main=$2&sub=$3


The reason I used the 'OR' is because I think my main problem arises when a URL applys to more than one Rule.

Please give me a hand.

Thanks a lot.
Erikina
 

Postby Caterham » Sun Apr 03, 2005 3:40 am

(.*)
Will match everything => your 1st rule matches fantastictutorials.com/CSS/basics/syntax.html, too, while $1 contains the blue part.
.* or .+ are the simplest RegEx, but they are causung the problems.

You have to modify the RegEx-Syntax. Use also Anchors anc escape charachers which need to be escaped

Code: Select all
RewriteRule ^([a-zA-Z0-9_-]+)\.html$   http://www.fantastictutorials.com/new.php?tut=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\.html$   http://www.fantastictutorials.com/catch.php?tut=$1&main=$2 [L]
RewriteRule ([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\.html   http://www.fantastictutorials.com/catch.php?tut=$1&main=$2&sub=$3 [L]
Use also L-Flags
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Thanks

Postby Eric » Mon Apr 04, 2005 1:46 am

Thank you very much. This will not be forgotten.
Eric
 

Postby Guest » Mon Apr 04, 2005 1:55 am

Thanks again. It works fantasticly.

If it's not too much trouble, would it be possible to make that include spaces ' ' and '&'? If it is, please don't worry about it.

Once again, thank you so much!
Guest
 

Postby Eric » Mon Apr 04, 2005 2:12 am

Sorry to triple post, but please don't bother with the above request as I'll use php to get rid of spaces.

But if it's no trouble allowing '&' and ':' would be great.

Thanks a lot
Eric Springer.
Eric
 

Postby Caterham » Mon Apr 04, 2005 3:44 pm

you can add the characters to the character class i.e.

Code: Select all
[a-zA-Z0-9&:_-]
instead of
[a-zA-Z0-9_-]


Robert
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 34 guests

cron