[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

htaccess rewrite rule - optional trailing slash

Posted on 2011-02-15
7
Medium Priority
?
1,020 Views
Last Modified: 2012-06-21
Hello,

I'm using this rewrite rule:

RewriteRule ^/artists/(.+?)/?$ /artists/artist.cfm?permalink=$1 [QSA]

So I have the option to go with or without a trailing slash in my URL - for example both of these URLs work AND pull the content from the database

http://ikonltd.com/artists/abakanowicz
AND
http://ikonltd.com/artists/abakanowicz/

My issue is when I click on either of the thumbs on the above URL, the content does not get pulled from the database on the next page:
http://ikonltd.com/artists/abakanowicz/1276/1/

If I don't allow the no trailing slash option:
RewriteRule ^/artists/(.+?)/ /artists/artist.cfm?permalink=$1 [QSA]

everything works fine. the client, however, wants the option of going with or without the trailing slash after the artist last name (permalink).

Here's both rewrite rules - again both work if the first one is
RewriteRule ^/artists/(.+?)/ /artists/artist.cfm?permalink=$1 [QSA]
INSTEAD OF
RewriteRule ^/artists/(.+?)/?$ /artists/artist.cfm?permalink=$1 [QSA]

CURRENT REWRITE RULES:
RewriteRule ^/artists/(.+?)/?$ /artists/artist.cfm?permalink=$1 [QSA]
RewriteRule ^/artists/(.+?)/(.+?)/(.+?)/ /artists/artwork_detail.cfm?permalink=$1&AutoArtID=$2&PageNum_newaqimages=$3 [QSA]
0
Comment
Question by:phillystyle123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34899159
The client is asking you to make two different things mean the same.  A trailing slash means that the server should look for a directory.  The absences of a trailing slash means that the server should look for a script.  It's been this way since about 1989 and while I normally agree that the client is always right, this request just does not make sense.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34899258
The way it the first rewrite rule was written, any URL that starts with /artists/ will be matched, so it is unfortunately also matching the content URL, which is http://ikonltd.com/artists/abakanowicz/1243/2/

Try this version instead, which should only match if there is a single subfolder after /artists/:
:
RewriteRule ^/artists/([^\/]*)/?$ /artists/artist.cfm?permalink=$1 [QSA] 

Open in new window

0
 

Author Comment

by:phillystyle123
ID: 34899488
sjklein42 - this works great

RewriteRule ^/artists/([^\/]*)/?$ /artists/artist.cfm?permalink=$1 [QSA]

the only issue i'm having is now the database content is not being outputed on this page:

http://ikonltd.com/artists/index.cfm

0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 16

Accepted Solution

by:
sjklein42 earned 2000 total points
ID: 34899593
I see two changes are needed at least - the first to properly handle the naked /artists/ path.

Also /artists/index.cfm/  -  presumably this should not be rewritten.

Assuming that real artist names do not have embedded dots, this version should be better.  It will only rewrite paths that start with /artists/ and have a single subfolder (with no dots in its name) followed by an optional trailing slash.

RewriteRule ^/artists/([^\/\.]+)/?$ /artists/artist.cfm?permalink=$1 [QSA]

Open in new window


This may not address your issue with content not showing.  When you say "content", do you mean images?
0
 

Author Closing Comment

by:phillystyle123
ID: 34899668
perfecto - thanks so much!

if you have a minute - could you break down the logic for the rewrite rule so i know what means what?

RewriteRule ^/artists/([^\/\.]+)/?$ /artists/artist.cfm?permalink=$1 [QSA]
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34899820
Breaking down this pattern:

^/artists/([^\/\.]+)/?$

The ^ means match start-of-string.
Then match exactly /artists/
Then the parenthesized part (...) will go into the $1 variable to be used in the substition.
Within the parens, we have:

[^\/\.]+

 the [^\/\.] means match anything EXCEPT (that's what the ^ does) a slash or a dot.  By habit I use the \ to "quote" the special characters / and . even though in this case the / probably does not need the \ before it

 the + means match one or more of them

 so together it matches everything up to the next dot or slash, whichever comes first and puts that string into $1.

Then back to the /? which matches an optional slash (or not)

Followed by $ which means match end-of-string.  So if there is anything unmatched left over at this point, the pattern fails (which is what we want it to do).

On the right side we use the $1 variable to plug the artist name we picked up with ([^\/\.]+) into the rewritten URL
0
 

Author Comment

by:phillystyle123
ID: 34899924
thanks tons for the explanation!
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question