Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite selectively changing url's from dynamic to static

my .htaccess is changing url's I don't want change

         

lgn1

5:39 am on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is my .htaccess file

RewriteEngine on
RewriteRule webscript/(.*)/(.*)/?$ /cgi-bin/webscript.pl?page=$1.html&cart_id=$2 [L]

My directory structure is:

- docs
- images
- cgi-bin

my .htaccess file resides under the cgi-bin directory and so does the script webscript.pl

now I get the desired effect

of dynamic urls going to

www.widget.com/cgi-bin/webscript.pl?&page=PAGENUMBER&cart_id=CARTNUMBER

when

www.widget.com/cgi-bin/webscript/PAGENUMBER/CARTNUMBER
is entered as the reference URL

What is messing me up is that all the images URL's which I don't want touch goes to

www.widget.com/cgi-bin/webscript/images/picture.jpg

instead of leaving the ./images/picture.jpg alone

I don't have a .htaccess file under my image directory, so shoun't my images being referenced be left alone.

Any ideas how to fix this?

lgn1

4:33 pm on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Still no luck getting this to work.

Do I need a RewriteCond to block ./image/picture and ../image/picture urls from getting rewritten, or
can this be done within the Rewrite rule?

jdMorgan

6:24 pm on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I don't have a .htaccess file under my image directory, so shoun't my images being referenced be left alone.

That depends on the current RewriteOptions inherit setting.

Also, you've used the very-ambiguous ".*" pattern, which will match anything equal to or longer that that URL-path you expect. I'd suggest:


RewriteCond %{REQUEST_URI} !\.(gif¦jpe?g¦png)$
RewriteRule webscript/([^/]+)/([^/]+)/?$ /cgi-bin/webscript.pl?page=$1.html&cart_id=$2 [L]

Change the broken pipe "¦" characters above to solid pipe characters before use; Posting on this forum modifies them.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com]

Jim

lgn1

7:38 pm on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Tried the modified code but its still rewriting the image url. I wonder if this has to do with the physical directory being different from the actual web directory. Im using Plesk on a 1&1 server, and this messes up the actual paths.

To my web browser:

/images
/docs
/cgi-bin

are all at the same root level under my domain

however physically on the disk

its

/home/httpd/vhosts/domain-name/cgi-bin
/home/httpd/vhosts/domain-name/httpdocs/images
/home/htptd/vhosts/domain-name/httpdocs/docs

could this be messing things up?

jdMorgan

3:18 pm on Jan 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, all warranties on code posted here are null and void if a 'control panel' is used -- They all do things 'differently' and it's impossible to keep up with all the variations.

The .htaccess code must be located in a directory in or above the directory containing the pages/images to be rewritten.

The RewriteRules in .htaccess will use the local URL-path, not the directory path, when deciding to rewrite or not.

So the .htaccess code location is filesystem dependent, while the code itself is URL-path dependent.

I hope that makes sense, that's the best I can explain it...

Jim

lgn1

5:13 pm on Jan 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aha.

I moved the .htaccess file to my root webserver directory (www.domain.com) which resides under /home/vhosts/httpd/domainname.com/httpdocs

and use the following code in my .htaccess file

RewriteCond %{REQUEST_URI}!\.(gif¦jpe?g¦png)$
RewriteRule ([^/]+)/([^/]+)/?$ /cgi-bin/webscript.pl/page=$1.html&cart_id=$2 [L]

and simplified my static request string to:

www.domain.com/PAGE/CART

and everything works.

When they say mod_rewrite is a difficult to learn as sendmail, they were wrong. mod_rewrite is like sendmail on drugs. At least with sendmail, you don't have to worry about its location.

And those d*mm unix/linux control panels, add more twists and turns with their own .htaccess files and htpd.conf variations, its enough to drive you insane.

After all this work, google better start deep crawling my site :)

Thanks JD for your input