Solved

htaccess rewrite rule - optional trailing slash

Posted on 2011-02-15
7
1,002 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 109

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 count occurrences of each item in an array.
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 …

773 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