Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

.htaccess mod_rewrite

New Here ,
Jan 17, 2010 Jan 17, 2010

I am attempting to use .htaccess to modify URLs/URL variables for seo purposes.  Specifically, I am trying to rewrite:

http://host.com/variable1/variable2.cfm

to

http://host.com/variable1.cfm?id=variable2

Unfortunately, I am failing miserably.  The following is what I have so far, and I am stuck.  Any suggestions that you could provide would be greatly appreciated.

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule /(.*)/(.*) /$1.cfm\?id=$2 [I,O]

829
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Jan 18, 2010 Jan 18, 2010

Your problem is undoubtedly in the pattern portion of your rewrite-rule.

Remember that regular-expression patterns can be "greedy" or "non-greedy,"which means that they can either seek to match the largest possible substring or the smallest one.

For example, ponder the potential differences that can occur when munching on "http://my.web.site/parm1/parm2/parm3" against a pattern of "(.*)/(.*)"  Your expectation of what will happen is probably "non-greedy."  But the bottom line is, "your expectation

...
Translate
Engaged ,
Jan 18, 2010 Jan 18, 2010

Your problem is undoubtedly in the pattern portion of your rewrite-rule.

Remember that regular-expression patterns can be "greedy" or "non-greedy,"which means that they can either seek to match the largest possible substring or the smallest one.

For example, ponder the potential differences that can occur when munching on "http://my.web.site/parm1/parm2/parm3" against a pattern of "(.*)/(.*)"  Your expectation of what will happen is probably "non-greedy."  But the bottom line is, "your expectations" don't mean squat.   Or rather, they amount to a huge waste of time and hair.  What you need to know, is to know.  And there's really only one way to find out  . . .

There are plenty of "try a regular expression" web-sites out there, where you can actually type-in a regex and a string and see what is or isn't matched.  Everyone (including both Apache and ColdFusion nee Java) uses compatible regular-expression engines these days, so you can pretty much "take your pick" of sites.

Regular expressions are always a surprise, but especially so when you start using constructs like ".*"  Save your hair-follicles and try it.

(I also do not see documentation of the 'I' and 'O' flags of RewriteRule.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 18, 2010 Jan 18, 2010
LATEST

It might be good to ask this on an Apache forum, given it's an Apache question.

One handy tool to get hold of when testing regexes is Regex Coach: http://weitz.de/regex-coach/

Also - I'm running from memory here - isn't there a way to log what Apache is doing when it's performing mod-rewrite operations so you can see where you're going wrong?  Or perhaps I'm thinking of IIS..?

Looking at your regex, the second sub-expression will also grab the ".cfm" part of the URL, won't it?  You don't want that: you want everything up to the ".cfm" in your sub-expression.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources