Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mod_rewrite 1

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
0
0
GB
I am trying to find a tutorial to learn about substituting the contents of a query string. All the tutorials I have seen so far, deal with redirection (which I have got to grips with) and then only give complex examples of the rewrite rule without much of an explanation.

Keith
 
Keep Googling. There are better examples out there.

What exactly are you trying to do?

 
I do hope there are better tutorials than the ones I have seen so far or we are all doomed :)
I will keep looking but I want to make addreses like
Code:
[URL unfurl="true"]http://www.mysite.co.uk/cgi-bin/script.pl?section=animals&code=B56546&desc=koala&sx=male[/URL]
into
Code:
[URL unfurl="true"]http://www.mysite.co.uk/animal/koala/male/B56546[/URL]
I asssume that I need split it on 'cgi-bin' and assign each param to $1, $2, $3 etc. but I need to know how to handle varying numbers of params.

This is a made up item, we do not sell koala bears

Keith
 
I have worked through numerous examples but I cannot get mod_rewrite to change the URL.

The relevant part of my latest .htaccess file looks like this.
Code:
RewriteEngine on
RewriteBase /
RewriteRule ^test1.htm$ test2.htm [L]
RewriteRule ^product/([0-9]+)/$ dummy.php?product_id=$1 [NC,L]

The first rule is working fine ie. redirects test1.htm to test2.htm but still displays test1.htm, all good so far.

Using a real life example
[URL unfurl="true"]http://www.studiosoft.co.uk/dummy.php?product_id=19[/url]

calls the following PHP file
Code:
<?php
$product=$_GET['product_id'];
print "Dummy PHP File<br>product_id=$product";
?>
The PHP file works as planned but the URL does not alter.
I have tried numerous examples with the same results.
I am obviously missing something important here and hope that someone can point me in the right direction.

Not being able to get a simple example to actually work is very frustrating.

Keith
 
The first rule is working fine ie. redirects test1.htm to test2.htm but still displays test1.htm, all good so far.

Well you do tell the server that this
Code:
RewriteRule ^test1.htm$ test2.htm [L]
is the last rule to process.

[L] (last rule)
Forces the rewriting processing to stop here and don't apply any more rewriting rules.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Thanks Chris
Thanks for taking the time to point out my error but I have tried endless extensions to no avail, I still can't get this 'simple' example to work.

Should this example work?
Code:
RewriteEngine on
RewriteBase /
RewriteRule ^product/([0-9]+)/$ product.php?product_id=$1

Keith
 
An even simpler version which uses no regex also fails to change the displayed URL.

My latest attempt was to change
Code:
[URL unfurl="true"]http://www.mysite.co.uk/cgi-bin/makepage.cgi?pag=webshop[/URL]
into
Code:
[URL unfurl="true"]http://www.mysite.co.uk/webshop.html[/URL]

using this rewrite rule created by a generator.

Code:
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]*)\.html$ /cgi-bin/makepage.cgi?page=$1 [L]
There must be a reason why this fails - could someone please give me a clue?

Keith
 
I would say that the problems you are having is that there IS more than one way to skin the proverbial cat, as no "right way" exists to accomplish redirects .htaccess.

First off read up on Regular Expressions, I have always found that this site to be very handy to have in your "favourites".
Reg Exp looks mind-numbingly complicated when first encountered but, honestly, once you do get into it the whole thing begins to become clear in your mind.
At High Rankings Forum we have an entire board just for redirects :D there are lots of examples there. Check the pinned thread in particular.

Also try and get out of the idea of using the [L] (last) flag so much, UNLESS there may be conflicting matches or conditions following.

more to follow ....

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
This:
Code:
[URL unfurl="true"]http://www.mysite.co.uk/cgi-bin/makepage.cgi?pag=webshop[/URL]

from this:
Code:
[URL unfurl="true"]http://www.mysite.co.uk/webshop.html[/URL]

Would be an alias rather than a rewrite (IMO). SO:

Code:
RedirectMatch 301 ^/([.*])\.html  [URL unfurl="true"]http://www.mysite.co.uk/cgi-bin/makepage.cgi?pag=$1[/URL]

should be work assuming I've worked that out in my head ok.

it "says" (or should say),

match a request starting from '/' on an atomic group of any character (.) any numder of times until ".html" is found. Then send it to:

[matched characters]



Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Thanks Chris
I am familiar with regex from using Perl but this URL changing malarky has me confused. I will have a look at the suggested examples and see if I can make any sense of them.

Keith
 
I have been reading about the subject and from what I have read, the simple examples I have tried should be working.


Asking an earlier question, should this rewrite rule work?
Code:
[URL unfurl="true"]http://www.mysite.co.uk/cgi-bin/makepage.cgi?pag=webshop[/URL] into
Code:
[URL unfurl="true"]http://www.mysite.co.uk/webshop.html[/URL]
Code:
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]*)\.html$ /cgi-bin/makepage.cgi?page=$1 [L]

Keith
 
I keep returning to this but I am finding it extremely frustrating as I cannot get the simplest of examples to work.

Code:
[URL unfurl="true"]http://www.mysite.co.uk/cgi-bin/makepage.cgi?page=cont.tmpl[/URL]

in the address bar with

Code:
RedirectMatch 301 ^/([.*])\.html [URL unfurl="true"]http://www.mysite.co.uk/cgi-bin/makepage.cgi?pag=$1[/URL]

in the .htaccess file does not alter the contents of the address bar.



Keith
 

Should change the url, if you type say: anything.html to anything
And redirect there.

What is it doing?





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
You may also want to remove the square brackets from your pattern to get it to match.

Code:
RedirectMatch 301 ^/([b][COLOR=#A40000][[/color][/b].*[b][COLOR=#A40000]][/color][/b])\.html [URL unfurl="true"]http://www.mysite.co.uk/cgi-bin/makepage.cgi?pag=$1[/URL]

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks Phil - You have helped me with the eureka moment.

I have been using this back to front!!! - what a chump!
It doesn't solve my problem but at least I have got it to do something.
I had it im my head that
Code:
[URL unfurl="true"]http://www.mysite.co.uk/cgi-bin/myscript.pl?pag=pagename[/URL]
would link to the URL above but display something pretty in the address bar like
Code:
[URL unfurl="true"]http://www.mysite.co.uk/pagename.html[/URL]
{/code]

I realise now that the redirect works the opposite way.
Now to figure out how to put
[code]
[URL unfurl="true"]http://www.mysite.co.uk/pagename.html[/URL]
in the address bar and while still displaying that, redirect to
Code:
[URL unfurl="true"]http://www.mysite.co.uk/cgi-bin/myscript.pl?pag=pagename[/URL]

I have a feeling that RedirectMatch 301 is not the right command for this application.

Keith
 
Instead of RedirectMatch 301 use RewriteRule.

Code:
RewriteRule ^(.*).html?$ /cgi-bin/myscript.pl?pag=$1 [NC,L]


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks Phil
I have been staring at this for ages and just could not get the concept.
Now, with your help, I have taken the first step, I am hoping the rest of the journey will be easier.

Keith
 
Its a little confusing at first I admit. And I'm no expert myself, only recently having to wrangle with this.

But in the end, once you get the regular expressions down, the Rules are easier to understand.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I found the biggest hurdle to get my head round was the basis on which it operates. I was approaching it from the direction of starting with a query string and trying to convert it to a basic URL but the solution is a direct opposite of that.
Doing this makes writing the query string within a server side script much easier too which is a bonus.

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top