Forum Moderators: coopster & phranque

Message Too Old, No Replies

lose ? using mod_rewrite

non programmer looking for assistence...

         

iggy99

9:08 pm on Apr 17, 2002 (gmt 0)

10+ Year Member



hello all -

i am looking for some help ---

we hope to "lose" the question mark in our url links---

the actual link - and others similar:

http://www.webshop101.com/alohaShop/?page=shop/reminders

i have tried to use:

RewriteEngine on
RewriteBase /
RewriteRule ^page([0-9]*).php /index.php?page=

we have mod_rewrite enbled on server -

see: http://www.webshop101.com/alohaShop/about/

using:

RewriteEngine on
RewriteRule ^(.+)$ [tropicalflowerworld.com...]

any suggestions would be much appreciated

(edited by: engine at 5:17 pm (utc) on April 24, 2002)

Olaf

11:55 pm on Apr 17, 2002 (gmt 0)

10+ Year Member



To get you started here is a snippet from my mod_rewrite include:
(PT means pass through, apache works as a proxy for the base address, user browser never knows that the base url exists)

RewriteEngine on

RewriteRule ^/icons/(.*)\.jpg$ /home/english/get_icon?v_numer=$1 [PT,L]
RewriteRule ^/pictures/(.*)\.jpg$ /home/english/get_mynd?v_numer=$1 [PT,L]

when the browser asks for /images/123.jpg it is sent to the script as /home/english/get_mynd?v_numer=123

No one is the wiser that the image was fed by a database.

And for scripts that require multiple values.

RewriteRule ^/english/pedigree/([^/]+)/printer/([^/]+)\.html$ /home/english/pedigree.printerfriendly?hund=$2&maxdepth=$1 [PT,L]

so an URL pointing to /english/pedigree/6/printer/123.html

will be served to the script as /home/english/pedigree.printerfriendly?hund=123&maxdepth=6

Hope it helps,

here is an link to an old quide to mod_rewrite (I found it really helpfull to start of reading that guide, and when later on I got more proficient, read the current one which is exponentially larger and more complex :) )
[wwwslap.cern.ch...]

Olaf

Olaf

7:06 pm on Apr 20, 2002 (gmt 0)

10+ Year Member



Ok, question about this topic.

I thought it would be a great idea to create a temporary rewrite base to redirect 301 all the original urls (since they are being referenced to by many homepages and search databases)

I tried to get it to work but no luck.

for straight conversions, no problem.

but for a cgi based page i just couldnt make it happen.

this is one example that I was trying to accomplish.

(this is what I tried, have tried variations, but none worked)
RewriteRule ^/english/frodleikuri\.gsd\?num=(.*)$ /english/structure_information/$1.html [R=301]

so that any incoming urls to /english/frodleikur.gsd?num=2 would get a Redirect (301) to /english/structure_information/2.html

Anyone knows the correct syntax for this operation?

Regards
Olaf

sugarkane

1:05 pm on Apr 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's a bit of a puzzler, Olaf, the syntax looks okay to me. I remember reading something about the PT flag and cgi scripts, but the details escape me. It might be worth adding that flag to the rule and seeing what happens.

Alternatively, you could try doing the same thing with the simpler RedirectMatch directive - less options, less chance of something going wrong ;)

RedirectMatch 301 ^/english/frodleikuri\.gsd\?num=(.*)$ /english/structure_information/$1.html

Let us know how you get on, or if you've already solved it!

Olaf

10:18 pm on Apr 26, 2002 (gmt 0)

10+ Year Member



Thanks for the reply
I tried RewriteMatch thingie but it didnt work. No redirect, just shows the orignal page/url :/ Which is really weird since my normal (static) redirects work fine. SO I know that its not to do with my multiple rewrites (I have a base rewrite sending all ^/english/... to ^/home/english/... with the [PT] flag

One wierd thing I am encountering is that with this setup:

RewriteRule ^/english/frodleikur\.gsd$ /english/structure_information.html [R=301]
RedirectMatch 301 ^/english/frodleikur\.gsd\?num=(.*)$ /english/structure_information/$1.html

This:
..../frodleikur.gsd?num=2
Gets a 301 to this:
..../structure_information.html?num=2

Even though the RewriteRule says ^/english/frodleikur\.gsd$ <- (notice $ stop)

And if I comment out the rewriterule it does not happen.

Olaf

bobriggs

6:29 pm on May 15, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Olaf...It took me a few hours of trial and error and digging to figure that one out. I had sort of the same kind of setup. Basically I didn't want a redirect, but I didn't want rogue spiders getting into my cgi so I was doing a backwards rewrite, that is pulling the parameters out of a query string but delivering a static html page. Most of the rewrites we see go the other way: pull the data out of the url and then build a query string.

That's the problem. The query string. It is not matched against, rather, it is stripped off and put in the environment.

So:
RewriteRule ^/english/frodleikuri\.gsd\?num=(.*)$ /english/structure_information/$1.html [R=301]

will never match because ?num= is not even being looked at.

Semi-Solution:
rewriteCond %{QUERY_STRING} ^num=(.*)
RewriteRule ^/english/frodleikuri\.gsd$ /english/structure_information/%1.html

Note the difference %1 - are matches in the preceding RewriteCond

Now your second problem is the redirect. Since the query string is saved in the environment, if you add the redirect, the server is going to add the original query string to the Location: header along with the newly rewritten URL, and it will show up in your browser. If you don't redirect, the .gsd?num= will show up in your browser, although the .html page will be served up. I haven't checked it but maybe it would be possible to use a setenvif statement to clear the QUERY_STRING environment variable.

Also, the reason the RedirectMatch doesn't work after the RewriteRule above is because the URL has already been rewritten. It looks like the RedirectMatch is what you're wanting anyway?