Solved

htaccess rewrite rule - optional trailing slash

Posted on 2011-02-15
7
996 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
  • 3
  • 3
7 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks tons for the explanation!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 …

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now