?
Solved

htaccess rewrite rule - optional trailing slash

Posted on 2011-02-15
7
Medium Priority
?
1,013 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What You Need to Know when Searching for a Webhost Provider
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

771 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