Welcome to WebmasterWorld Guest from 54.87.18.165

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

mod rewrite issue

am i causing a loop?

     
10:34 pm on May 31, 2009 (gmt 0)

New User

5+ Year Member

joined:May 31, 2009
posts: 32
votes: 0


I've been trying to figure this out for a while now and know I'm missing something really simple. I want to take a URL like

www.example.com/product/category/type

and have it pull up

www.example.com/product.php?cat=category&type=type.

I understand the basic concept, my problem is what I think is me creating a loop that I don't know how to get around. My .htacess code is:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^product/(.*)/(.*)$ product.php.cfm?cat=$1&type=$2 [L]

Unfortunately, if I enter

www.example.com/product/category/type

it returns a 500 internal server error, but if I change my .htaccess file to read

RewriteRule ^products/(.*)/(.*)$ product.php.cfm?cat=$1&type=$2 [L]

and enter the new URL, it pulls up the page fine. I'm guessing it's choking because product (singular) in the file keeps getting rechecked. How do I get around this? Thanks!

11:49 pm on May 31, 2009 (gmt 0)

Senior Member from CA 

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

joined:Aug 31, 2003
posts:9074
votes: 6


Welcome to WebmasterWorld sdguy :) This is a guess, but does it make any difference if you change the Options directive to read:

Options +FollowSymLinks -MultiViews
12:05 am on June 1, 2009 (gmt 0)

New User

5+ Year Member

joined:May 31, 2009
posts:32
votes: 0


Woohoo! Will have to experiment a bit but it looks like that did it. I knew it was something like that, can I expect any other affects from -MultiViews?

Thanks for the help!

12:16 am on June 1, 2009 (gmt 0)

New User

5+ Year Member

joined:May 31, 2009
posts: 32
votes: 0


Urg, looks like I still have a problem. My pattern, which is actually this:

RewriteRule ^date-ideas/(.*)/(.*)$ date-ideas.cfm?cat=$1&dID=$2 [L]

seems to now match and replace for all of my other pages (ie a link to fun-date-ideas.cfm is now date-ideas/category/fun-date-ideas.cfm)

Maybe my original question should have been how do I get mod_rewrite to ONLY match a term like "date-ideas" and disregard "fun-date-ideas", "date-ideas.cfm", etc? Or am I still confused? Boy this is a lot more difficult than it seems like it should be.

1:10 am on June 1, 2009 (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


There's no magic here. Your rule

RewriteRule ^date-ideas/(.*)/(.*)$ date-ideas.cfm?cat=$1&dID=$2 [L]

does not match the requested URL-path "fun-date-ideas.cfm".

Therefore, it is not this rule that is causing the problem unless that is not the exact rule or the exact requested URL-path (the link on your page).

The only thing I see wrong with your rule is a "sloppy" pattern that is also very inefficient. Avoid the use of ".*" sub-patterns in order to prevent unexpected results -- A sub-pattern of ".*" matches anything, nothing (blank), and everything. Putting more than one is a pattern is also hugely inefficient, causing dozens, hundreds, or even thousands of 'back-off-and-retry' matching attempts. Too many of these can kill a busy server. (If you see such a multiple-".*" pattern recommended in a forum, run away.)

Instead, try a much more specific pattern such as

 RewriteRule ^date-ideas/([^/]+)/([^/]+)$ date-ideas.cfm?cat=$1&dID=$2 [L] 

This matches all characters from after the first slash up to the second slash into $1, and everything after that slash into $2, but will not match a requested URL if it contains a third slash. You could make it even more specific, excluding URL-paths with a filetype (indicated by a period) in the final URL-path-part, by also excluding periods in the second subpattern:

 RewriteRule ^date-ideas/([^/]+)/([^./]+)$ date-ideas.cfm?cat=$1&dID=$2 [L] 

If these patterns are not clear, take a look at the resources cited in our Forum Charter, especially the regular-expressions tutorial.

Jim

1:22 am on June 1, 2009 (gmt 0)

New User

5+ Year Member

joined:May 31, 2009
posts:32
votes: 0


Hi Jim, thanks for the reply. About the matching, that's where I got confused too, because my pattern (with the /'s) doesn't seem to match the generic links in my navigation but if I pull up a page with that pattern they all change.

<snip> the links in the nav menu on the left are fine, but if you then pull up a page with the pattern I'd like to use, ie http://www.example.com/date-ideas/romantic/page the nav links all get mucked-up.

[edited by: jdMorgan at 1:46 am (utc) on June 1, 2009]
[edit reason] No URLs, please. See Terms of Service. [/edit]

1:27 am on June 1, 2009 (gmt 0)

New User

5+ Year Member

joined:May 31, 2009
posts:32
votes: 0


Btw, my .htaccess file is now

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteRule ^date-ideas/([^/]+)/([^/]+)$ date-ideas.cfm?cat=$1&dID=$2 [L]

1:50 am on June 1, 2009 (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


If the nav links are getting mucked-up, it's likely because you have specified page-relative links instead of server-root-relative links or canonical links. Link to "/page-path-from-sever-root" or http://www.example.com/page-path-from-sever-root. In other words, do not use on-page links that don't start with a slash or include the full protocol and domain. This applies to images, css files, included JS files, and everything.

It may help to clarify this problem if you understand that it is the client (e.g. the browser or robot) that resolves relative links into the full URL that it must request. If you use page-relative links, the browser takes the URL in its address bar, strips off the "file" (if present) leaving the full directory path, and then adds your relative link to that. By using 'fake directories' as your links, you are telling the browser to resolve relative links based on those directory paths.

Jim

[edited by: jdMorgan at 2:12 am (utc) on June 1, 2009]

2:34 am on June 1, 2009 (gmt 0)

New User

5+ Year Member

joined:May 31, 2009
posts:32
votes: 0


That was it Jim, thanks for the explanation. It looks like I may have some problems passing URL variables between pages now, but this is a great start.