Welcome to WebmasterWorld Guest from 54.156.39.245

Forum Moderators: bakedjake

Message Too Old, No Replies

mod_rewrite (Again!)

Having a few teething problems with the mod_rewrite module.

     
12:01 pm on Feb 26, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2003
posts:1023
votes: 0


Hi guyz,

Firstly sorry for another post regarding the apache mod_rewrite module, but am pulling my hair out at this one!
Anyway... many thanks for all help that you might be able to assist me with.
What i would like to do with the mod_rewrite module is have a URL as:
[mydomain.com...] into the broswer which would then call on this page:
[mydomain.com?country=scotland&type=tour&area=grampian&town=buckie...]
obviously there is several hundred possible URL's to which i would like to redirect.
I have completed a simple redirect that goes from fred.html to nohead.html (from an online example), first thoughts were to have a redirect for each possible outcome of the above senario, which would create a large (pointless) .htaccess file.
Is it possible for me to actually query each URL inbound and then process this from a query on each directory within the requesting URL i.e. using the above example: s would know that the country was scotland where t would know that it was a query on my tour category and so on to the last where the *.html file would populate the final query again using the above town would equal buckie!

So far my mod_rewrite .htaccess file looks like the following:
RewriteEngine on
RewriteBase /
RewriteRule ^/~([a-z])/([a-z])/([a-z])/([a-z])\.html$ Navigate.do?country=$1&type=$2&area=$3&town=$4

But when i test it out nothing happens, i have tested that the rewrite module is switched on with some examples i found on webmasterworld using the fred.html nohead.html which works!

Once more thanks and keep up the good work guyz :)

Best wishes,

George

12:51 pm on Feb 26, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 22, 2002
posts:2546
votes: 0


You can use back references to rewrite the urls. The only problem is you can only have two of them, so you will have to combine some of the variables with a delimiter and then split them in your script.

Something like this:

Url to rewite-http://www.mydomain.com/scotland-tour-grampian/buckie.html

*Notice the dashes. They are the delimiters.

RewriteEngine On
RewriteRule ^/domain\.com/(.*)/(.*)$ /?cta=$1&town=$2[L]

Now you have rewritten the url to:

domain.com?cta=scotland-tour-grampian&town=buckie

Now, split the cta variable up(I'll use Php):

$cta = explode($cta);
$country = $cta[0];
$type = $cta[1];
$area = $cta[2];

Now you have all your vcariables in place, ready to query the db.

*NOTE-If you have static pages, this will also rewrite them:(

To fix: Put you script on another page, rather than index:

h*tp://www.mydomain.com/script.php/scotland-tour-grampian/buckie.html

Then you rewrite rule also changes to:

RewriteEngine On
RewriteRule ^/domain\.com/script.php/(.*)/(.*)$ /?cta=$1&town=$2[L]

Hope that helps you understand better.

12:58 pm on Feb 26, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 0


If you have access to httpd then you might want to look at the RewriteMap [httpd.apache.org] directive which will suite your job just fine.

Andreas

2:12 pm on Feb 26, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2003
posts:1023
votes: 0


Many thanks for the prompt reply guyz!

In answer to andreasfriedrich, I donít have access to the httpd :( only capable of using the .htaccess, but at the same time the link you have provided is useful for furthering my knowledge of mod_rewrite thanks...

I have a few queries regarding Birdman's post if you donít mind that is!

<I>RewriteEngine On
RewriteRule ^/domain\.com/(.*)/(.*)$ /?cta=$1&town=$2[L]

Now you have rewritten the url to:

domain.com?cta=scotland-tour-grampian&town=buckie</I>

I totally understand what is going on here, but then you go on to mention:
<I>Now, split the cta variable up(I'll use Php):

$cta = explode($cta);
$country = $cta[0];
$type = $cta[1];
$area = $cta[2];</I>

Am not familiar with PHP as I am using JSP, the above code refers to...? i.e. do I carry out my condition & split (above script) within the .htaccess file or does that code belong within the PHP script?

Once again thanks folks :)

George

2:20 pm on Feb 26, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 22, 2002
posts:2546
votes: 0


>>>do I carry out my condition & split (above script) within the .htaccess file or does that code belong within the PHP script?

You will split the cta variable up on your php or jsp script. ie. the script that queries the db.

2:38 pm on Feb 26, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2003
posts:1023
votes: 0


once again thanks Birdman.
11:56 am on Feb 27, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2003
posts:1023
votes: 0


Right guyz I have the mod_rewrite fully working now!

I had to change the .htaccess to:
rewriteEngine on
rewriteBase /test
rewriteRule ^(.*)/(.*)$ /Redirect.do?cta=$1&town=$2[L]

So once again I wish to thank everyone that assisted me in this :)

I have another small problem and am not sure if I am directing it to the appropriate category, so I apologise in advance. My defaultTemplate that the above Redirect method calls on is working fine until it tries to display my images:
<img src="images/pic.jpg">

But when the URL path becomes domain.com/scotland-tour-angus/dundee.html

The image is expected to be located in domain.com/scotland-tour-angus/images/pic.jpg

I am running my site on resin (application server) using the JSP struts template architecture.

Any help on this matter is magic :) and once more thanks in advance

George

10:23 pm on Feb 28, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2003
posts:1023
votes: 0


Hi again :)

Looking for advice regarding my mod_rewrite that was shown above in a working state, when placed in my testing directory, got it fully operational along with my event processor. But when I moved it into the root of my directory as shown below:
[.htaccess]
rewriteEngine on
rewriteBase /
rewriteRule ^(.*)$ /Redirect.do?cta=$1&town=$2 [L]
<B>OR</B>
rewriteEngine on
rewriteRule ^(.*)$ /Redirect.do?cta=$1&town=$2 [L]
[.htaccess]
I think it falls into an infinite loop or something as my index page never loads (not sure on it being an infinite loop though, as am not sure on how to debug it!)

Or is there some other reason that is obvious to the expert eye!?

I again say I can't thank you enough folks for the help.

KR

-gs

10:39 pm on Feb 28, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 22, 2002
posts:2546
votes: 0


I don't think you need the "rewriteBase /" line. Also, you are trying to recall two back references whereas you only defined one.

Could you show the format of the url before it is rewritten?

10:47 pm on Feb 28, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2003
posts:1023
votes: 0


Sorry Bird the .htaccess file should have read:
[.htaccess]
rewriteEngine on
rewriteRule ^(.*)$ /Redirect.do?cta=$1 [L]
[.htaccess]

My head is in a disarray with it :)

-gs

11:06 pm on Feb 28, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2003
posts:1023
votes: 0


Just another thought am looking at and wondering if it is worth looking at further is known problems with running mod_rewrite under Resin my servlet and JSP engine. Is this worth going into or am I wasting my time there?

Thx

-gs

12:03 am on Mar 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2003
posts:1023
votes: 0


[more info]

When pinging the server (with above .htaccess in place) i get the echo back! but when i tracert it i get negative hops back...

Weird stuff...!

-gs

12:10 am on Mar 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 22, 2002
posts:2546
votes: 0


rewriteEngine on
rewriteRule ^(.*)$ /Redirect.do?cta=$1 [L]

That should redirect a request for:

yourdomain.com/your-variables-here

To:

yourdomain.com/Redirect.do?cta=your-variables-here

You don't need: [.htaccess]

(not sure if that was just for latout or if you put it in the file)

12:18 am on Mar 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2003
posts:1023
votes: 0


Yeah it was just a layout feature Birdman ;-)

I have also seen a posting on the apache site suggesting the use of a full uri:
rewriteRule ^(.*)$ [mydomain.com...] [L]

Tried it out, but again with the same issues as above!

Is the only place I can view any errors in my server logs? And can I debug this code in anyway, with the use of debug statements?

-gs

12:37 am on Mar 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


To prevent a loop:

RewriteEngine on
RewriteCond %{REQUEST_URI} !Redirect\.do
RewriteRule ^(.*)$ /Redirect.do?cta=$1 [L]

Jim

12:48 am on Mar 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2003
posts:1023
votes: 0


That did the trick Jim, thx! :)

But...

It can handle both requests either from my domain.com or domain.com/scotland-tour-angus-dundee.html, when the page loads all the paths are out of range my style sheets are not displayed along with images. Any further thoughts on the matter are appreciated.

-gs

1:18 am on Mar 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Alternative_Future,

I'd suggest using links which are relative to your root directory, i.e. use 'img src="/page_dir/image.jpg"' instead of 'img src="image.jpg"' on each page. You can view the first preceding slash as saying, "to find this image (or CSS file), go to the site's root directory, and start from there." This gives a "fixed address" for the image/CSS files, even though the page "thinks" it has been moved.

You could also use a 'base href="http://www.mysite.com/page_dir"' metatag on the page(s), instead of editing all the links.

I hope I understood the question!

Jim

1:43 am on Mar 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2003
posts:1023
votes: 0


Here are the results Jim:
I put the <base href.../> in place worked perfectly with a request to mydomain.com (without the .htaccess in place), when placing the .htaccess file in place and requesting mydomain.com [Failed] same error as above no styles in place (currently my directory structure does account for absolute paths rather than relative, by using a dir in the root /pictures/ same rule for the /styles/), but when the requesting uri is mydomain.com/scotland-tour-angus-dundee.html it works perfectly 8()
It's 1:30 am here now, perhaps a good nights sleep will help me come up with some other reasonable explanation :)
Thanks to everyone for all help received upto date...

-gs

It's nice to be important, but it's more important to be nice.
<I>By someone nice</I>

1:57 am on Mar 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Alternative_Future,

Well, in the morning, check your error log and see what path the images and CSS were requested from - The error log will show the complete file path, and will likely show you where it's going wrong.

Best,
Jim

4:23 pm on Mar 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2003
posts:1023
votes: 0


Jim,

Done what you suggested, checked the server logs everything looked fine, I then output to the browser my servername and requestURI, again fine.
This made me look again at my event processors, one of which was inside an exception -Redirect.do- (which had not been handled properly and redirected the page to a custom 404 error) I then realized I had never accounted for an entry here other than when the requesting path was mydomain.com/blah-blah-blah-blah. I have now added a condition to the above EP dealing with an incoming request from my root i.e. an action forward statement to the index.jsp.
But somehow it still displays without the stylesheet, even when I use an absolute path:
<link rel=stylesheet type="text/css" href="http://www.mydomain.com/styles/style.css">
Would and is it possible for me to handle an incoming request without the parameter cta inside my .htaccess?
i.e.
if(cta == null){
just go to mydomain.com avoiding the use of any EP's
}
I know the syntax is well off here ;) But hopefully it makes sense! This question is aimed to all :)

KR,

-gs

4:51 pm on Mar 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 22, 2002
posts:2546
votes: 0


Hello,

That was what I meant near the bottom of message 2 [webmasterworld.com].

I think if you move that script out of root and into it's own folder, you will be okay. Then it will only rewrite urls with your script name in them.

Hopefully Jim will stop in because he is far more knowledgable than I.

fixed link

5:24 pm on Mar 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2003
posts:1023
votes: 0


Hi Bird,

I see the logic now that you refer me back to it, totally forgot about that :(

I then went ahead and tried out with the following .htaccess files:
RewriteEngine on
RewriteCond %{REQUEST_URI}!Redirect\.do
RewriteRule ^/Redirect.do/(.*)$ /Redirect.do?cta=$1

<B>AND</B>

RewriteEngine on
RewriteCond %{REQUEST_URI}!Redirect\.do
RewriteRule ^/Redirect.do/(.*)$ /?cta=$1

When requesting either .htaccess with the uri [mydomain.com...]

I get this error:
400 Invalid path /blah-blah-blah-blah.html was requested

If it were also possible I would rather keep the Redirect.do out of the request uri.

I will try a few more alternatives with the suggestion you have made though :)

Thx,

-gs

7:26 pm on Mar 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 22, 2002
posts:2546
votes: 0


Hello,

Try this and make sure "Redirect.do" is in a folder called "test".

RewriteEngine on
RewriteCond %{REQUEST_URI}!Redirect\.do
RewriteRule ^/test/Redirect.do/(.*)\.html$ /test/Redirect.do?cta=$1

4:25 pm on Mar 5, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2003
posts:1023
votes: 0


Hi Birdman,

Firstly I am sorry for the delayed response to your last posting helping me out. In the end I placed my .htaccess in another directory so that it became mydomain.com/dir/blah-blah-blah.html which seems to work fine (my only concern here was that it might have an effect on the SE/UA's, but after reading up more on them I should be ok :)

I did try out what you suggested had a few teething problems but nothing that would not have sorted if I had spent more time on it!

Anyway a massive thanks going out to everyone @ webmasterworld for their help & support with a special thanks to Birdman, andreasfriedrich & jdMorgan.

-gs

9:09 pm on Apr 8, 2003 (gmt 0)

New User

10+ Year Member

joined:Apr 1, 2003
posts:9
votes: 0


Hello, y'all!

I'm desparately seeking a solution to something I have been frustratingly trying to solve for more than a week now. From a search on this site I found this thread and it seems to come close to addressing the arena of my problem.

would you participants to this thread be willing to show me how I can accomplish the following?

URL entered by user: members.mysite.org/bozo

[note the absense of a filename extension. this is because "bozo" is a username in a database]

REDIRECTED URL should end up being: members.mysite.org/index.php?username=bozo

SUCH THAT the index.php page will use the username ("bozo" in this case) as input to a MySQL query to build the appropriate PHP/dynamic page.

CAN YOU HELP?!? 8-/

>>> Now, I have asked about the solution to this in a previous question (http://www.webmasterworld.com/forum13/2358.htm?highlight=not+a+bozo) but perhaps this rewrite of my promblen will be more explanatory - and reach others who have discussed this very same idea in this thread!

Please, PLEASE help! ... I am so SOOOOOO frustrated and stuck!

much appreciation in advance for all your help,

glen

9:18 pm on Apr 8, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


does it matter to you if members.mysite.org/index.html gets rewritten to members.mysite.org/index.php?username=index.html?

In other words are there any real files or directories under members.mysite.org, or do you want all accesses to the members subdomain redirected to the script?

Jim

10:18 pm on Apr 8, 2003 (gmt 0)

New User

10+ Year Member

joined:Apr 1, 2003
posts:9
votes: 0


Thank you for your question, jdMorgan!

And, No, it does not matter what happens to the index.html within the subdomain members.occamft.org. All I really want is a quick way to have a user's single page referenced by the use of their name in the URL. That could look like:

jdmorgan.mysite.org - OR -
members.mysite.org/jdmorgan - OR -
? almost ANYTHING ... But NOT:

www.mysite.org/jdmorgan.html - OR -
www.mysite.org/index.php?username=jdmorgan

I want this as simple as possible for the user's single page to be accessed by anyone (the page itself will be constructed based on PHP/MySQL accessing a database). 8-)

does this sound whacky? Am I asking for a fire department hook & ladder truck when all I need is a foot stool? ;-)

glen

10:35 pm on Apr 8, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


not_a_bozo,

If you have a separate directory that you use for the subdomain "mambers", and this code is to be placed in .htaccess in that separate directory, then you can delete the first RewriteCond line of this code - it will already exist in your root directory .htaccess or in your httpd.conf. Also, you may already have one or both of the first two lines, "Options" and "RewriteEngine", in which case those should not be repeated and can also be removed from this snippet.


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^members\.mysite\.com [NC]
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^(.*)$ /index.php?username=$1 [L]

Sorting out the jdmorgan.mysite.com variant will require you to define all of the other non-user subdomain names, i.e. "www", <blank>, "members", and anything else that would be an invalid member name. But it's not really hard once the problem is well-defined. mod_rewrite has been described as a swiss army knife, and indeed it is!

More info: Introduction to mod_rewrite [webmasterworld.com]

HTH,
Jim

11:39 pm on Apr 8, 2003 (gmt 0)

New User

10+ Year Member

joined:Apr 1, 2003
posts:9
votes: 0


Jim,

that does the trick! am now able to enter "members.mysite.org/username" and have username's individual page pop up! Hooray!

I am SO SO SO relieved! Many many thanks (I've no real way of expressing my humblest appreciation for this help from you!) ...

I've been pouring over all the retrieved threads from a search on "rewrite" (might have been "redirect") and found at least a dozen threads to read through for more information today. ... that is to say, I've previously read the introduction_to_mod_rewrite at least once. the first time was a few days ago. I thought it would solve my dilemma. ... in effect it did. But, not until after the proper set of scripted lines were used in the .htaccess file. Here's what I eventually used from your offer:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI}!index\.php
RewriteRule ^(.*)$ /index.php?username=$1 [L]

I did remove the first RewriteCond line. The above four lines worked as soon as it was removed.

Now that I have one working model, and have had experience with many many NON-working attempts, I believe I will be able to reread the mod_rewrite tutorial with a tad more understanding and may be able to venture into other things.

Other than the online manual and this tutorial, is there a hardcopy, a book which is as informative but not as 'technical' - you know, something more 'user friendly'? 8-)

I'm done now with that phase of my site-building. I am happy and relieved. I am greatful to you, Jim!

take care, and YTH! (Yes That Helped!)

glen

11:54 pm on Apr 8, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Glen,

Glad that helped.

I'm not aware of any user-friendly documentation, other than the on-line Apache documents. I just read that and kept experimenting (in a private subdomain) until I understood the Apache mod_rewrite docs.

I've no real way of expressing my humblest appreciation for this help from you!

When and if possible, support this site by becoming a member!

Jim

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members