Forum Moderators: phranque

Message Too Old, No Replies

Rewrite rules not working

mod_rewrite rules for search engine friendly urls not working

         

Skitso

6:55 am on Jul 12, 2004 (gmt 0)

10+ Year Member



Hey Everybody!

I have a few more problems. I've followed several tutorials - the one here at webmasterworld, one at sitepoint, and some from the apache docs for mod_rewrite (but most of that was over my head), but I can't get this rewrite scheme to work. Its one of the last few remaining pieces holding me back from releasing the next version of my site.

RewriteRule /gallery /gallery.php
*I want the url to gallery.php to just be /gallery

RewriteRule /gallery/([a-zA-Z_]) /gallery.php?celeb=$1
*I want a url like /gallery/Hereisa_Fakename to be rewritten, but only if the name contains letters and underscores else it defaults to gallery.php (how do I get a default if the rewrite doesnt wrk?)

RewriteRule /gallery/dl/([0-9a-zA-Z_\.-;&]+)/([0-9]+) /gallery.php?dowot=downloadpix&pix=$1&cat=$2
*A link like [localhost...] will download a file, but only if the filename contains letters, numbers, periods, dashes, & sybols, semi-colons, and nothing else.

RewriteRule /gallery/full/([0-9a-zA-Z_\.-;&]+)/([0-9]+) /view.php?dowot=viewpix&pix=$1&cat=$2
* uses the same url checking as gallery/dl only this time the server shows the file as an inline image

***have not implemented these yet...
RewriteRule /gallery/([a-zA-Z_])/([0-9]+)/([0-9]+) /gallery.php?dowot=viewalbum&celeb=$1&cat=$2&page=$2
* for browsing the gallery to next page

RewriteRule /gallery/([a-zA-Z_])/([0-9]+)/([a-zA-Z])/([a-zA-Z])/([0-9]+)/([0-9]+) /gallery.php?dowot=viewalbum&celeb=$1&cat=$2&orderby=$3&arr=$4&ppp=$5&page=$6
*for browsing the gallery on user seelected vars (prefs)

The problem here is none of what I want to work works. I can get the first rule to work, but never any of 4 the others I have tried.

Since my attempts haven't worked I'm assuming I've screwed something up. Can someone look through the first 4 rules to see if anything obvious jumps out?

Thanks so much!

...these darn people that coded the gallery I'm using made things hard on me...they have scripts that routinely parse more than 3 get vars...I'm trying to make my site indexable. I hope I'm on the right track, at the very least.

Skitso

10:57 am on Jul 12, 2004 (gmt 0)

10+ Year Member



I don't see an edit link, so I'll just give an update.

What I've got that works:
RewriteRule ^gallery(/)?$ /gallery.php
RewriteRule ^gallery/([a-z_]+[^0-9])$ /gallery.php?celeb=$1 [NC]

*how do I get it to default to gallery.php if someone inputs an invalid name?

What I'm Stuck On:
RewriteRule ^gallery/dl/([0-9]+)/([read below]{4,255})$ /gallery2.php?dowot=downloadpix&pix=$2&cat=$1 [NC]

* I'm going to search more on regex andspecial chars. I want it to allow all characters other than those forbidden (/\:*?"<>¦) by windows and I want the file to be between 4 and 255 characters (anybody know the max # chars for windows? I thought it was 255, I'm unsure though).

Skitso

1:05 pm on Jul 12, 2004 (gmt 0)

10+ Year Member



since my script loads files and these filenames must be passed through mod_rewrite, how do I treat (&) ampersands in a filename?

also, I discovered, aside from any syntax errors, why none of my mod_rewrite rules were working - the rewrite was /gallery/ on localhost my page is gallery.php in the root dir. I turned off rewriting and typed /gallery by accident and my gallery script showed - how can I stop this? It is interfereing with my rewrites so that even if I did stiff right, it'd not work.

jdMorgan

2:53 pm on Jul 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This may be the key:

> The problem here is none of what I want to work works. I can get the first rule to work, but never any of 4 the others I have tried.

You will need to add the [L] flag to each rule unless you want the output of that rule processed by the rules that follow it. Then order your rules from most specific to least specific.

As you have written your code, the first rule matches any URL that contains "gallery" and changes the URL to "/gallery.php", and therefore none of the rest of the rules will match.

Jim

Skitso

3:06 pm on Jul 12, 2004 (gmt 0)

10+ Year Member



I have made those changes, though that was not a hinderance on this computer - for some reason the rules worked any how.

How do I deal with the & sign? It chokes up the server and spits out a 404.

And why, when I type in /gallery, goes it go to gallery.php when mod_rewrite is not on?

I just installed the newest versions of PHP, Apache, and MySQL, so I don't think it has anything to do with server setting, I haven't customized them at all (other than to ser DOC root, and to enable modrewrite.)

Skitso

5:59 pm on Jul 12, 2004 (gmt 0)

10+ Year Member



This forum needs an upgrade bad. I could fit all this crap onto one post if I had the ability to edit my own main topic post!

I posted to say one of my problems (apparently multiviews was enabled) is described better here:
[webmasterworld.com...]

Skitso

12:34 am on Jul 15, 2004 (gmt 0)

10+ Year Member



Ok. Another update:

my url structure looks like this:

/pictures
/pictures/Category-Name
/pictures/Category-Name/dl/filename.jpg (download prompt)
/pictures/Category-Name/full/filename.jpg (download img in browser)

My question is, will the way the last two urls are setup confuse a search engine? The url will look like an image file itself, but is merely passing that to another script.

All other problems have been resolved regarding this post...though I have to say most was done via trial and error and hours of reading through other posts.

jdMorgan

1:14 am on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> My question is, will the way the last two urls are setup confuse a search engine?

No. If you are doing internal rewrites, the spider will never know that a script is involved. As long as your structure yields URLs that can easily be parsed by mod_rewrite regex patterns, you'll be fine. In this case, the additional slash and the presence of a filetype (indicated by a "." in the URL-path) are good enought write reliable mod_rewrite patterns.

Spiders don't see anything that a browser doesn't see (However, this does include the response headers, MIME-types, caching info, etc., that a browser does see, but does not usually display to a surfer). Therefore, there is no way a spider can tell the difference between static and dynamic content on a properly-implemented site.

Jim

Skitso

1:59 am on Jul 15, 2004 (gmt 0)

10+ Year Member



Ok. Thanks for your help. Seems I wrote too much junk for everyone else.

This is what I am going with. I decided against filenames in the url though I might now show them on my gallery page so the user knows if they are dl-ing a duped image, etc.

The first two rules are equally specific...then on down to less specific rules as suggested (at least i think I understood properly.)


RewriteEngine on
RewriteRule ^pictures/([a-zA-Z-]+[^0-9])/full/([0-9]+)$ /view.php?dowot=viewpix&celeb=$1&pix=$2 [L]
RewriteRule ^pictures/([a-zA-Z-]+[^0-9])/dl/([0-9]+)$ /gallery.php?dowot=downloadpix&celeb=$1&pix=$2 [L]
RewriteRule ^pictures/([a-zA-Z-]+[^0-9])/([0-9]+)$ /gallery.php?dowot=viewalbum&celeb=$1&page=$2 [L]
RewriteRule ^pictures/([a-zA-Z-]+[^0-9])$ /gallery.php?dowot=viewalbum&celeb=$1 [L]
RewriteRule ^pictures$ /gallery.php [L]

I'd just like confirmation that the above rewrite rules are valid and safe for a production enviroment.

A few wish items:
1.)is it possible to redirect requests for urls that are not valid to a "base" url?

*user enters [site...]
*User gets 404
*Instead of the 404, I'd like to take user to a specified page (one or two "dirs" back which will always be an index) based on the mod_rewrite spitting the url out. I'd rather there not be a chance for a 404 to be sent.

ex.2
[localhost...]
op triggers 404
user ends up in 404 (prob by their own doing)
I'd like user to end up at /Cat-name

2.) A way to accept both "/" slash and no slash in the rules. I have found that if I accidentally add a trailing slash an error occurs. I could, at the beggining of my gallery script, remove a trailing slash, but I think it'd be smoother if I could do it before the request "gets" to the file. The last way I tried this was, but I


RewriteRule ^gallery(/)?$ /gallery.php

I hope I'm not clouding this up more :(

Thankyou for your help jdMorgan.

jdMorgan

5:48 am on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1.)is it possible to redirect requests for urls that are not valid to a "base" url?

*user enters [site...]
*User gets 404
*Instead of the 404, I'd like to take user to a specified page (one or two "dirs" back which will always be an index) based on the mod_rewrite spitting the url out. I'd rather there not be a chance for a 404 to be sent.


The simplest method is to use ErrorDocument to define a custom 404 error page. This can be done on a per-directory basis. The advantage is that it does provide a 404 response, which prevents you from messing up your search engine listings with duplicate content and the resultant confusion about which URL is "real".

The other method is to use the -f and -d options of RewriteCond to test whether files and directories exist, and do redirects/rewrites based on that. The downside is that it is inefficient, and you'll generate 301, 302, or 200-OK responses for missing or removed files -- and that, IMHO, is a big mistake because it violates the HTTP "specification". When you do that, you must accept that it can cause a "furball" of problems later (many of which we get to "repair" here after sites get dropped or URLs get mixed up in search, etc.) I am an unabashed believer in going by the book with HTTP responses, BTW.

2.) A way to accept both "/" slash and no slash in the rules. I have found that if I accidentally add a trailing slash an error occurs. I could, at the beggining of my gallery script, remove a trailing slash, but I think it'd be smoother if I could do it before the request "gets" to the file. The last way I tried this was, but I

RewriteRule ^gallery(/)?$ /gallery.php


That question got cut off, somehow, so I may be missing some details here. But using just

RewriteRule ^gallery/?$ /gallery.php [L]

should work fine.

Jim

Skitso

12:01 pm on Jul 15, 2004 (gmt 0)

10+ Year Member



Wow, thanks for pointing out the problem with my non-404 idea. I should have thought of that in the first place. I definately don't want problems anywhere down the road, and duplicated content is a problem...

Jim, thank you very much for the help. I'm pretty sure if I had not asked for help I would have done something terribly bad with my site.