Solved

htaccess rewrite rule - optional trailing slash

Posted on 2011-02-15
7
1,009 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 110

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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