[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

htaccess rewrite rule - optional trailing slash

Posted on 2011-03-04
7
Medium Priority
?
957 Views
Last Modified: 2012-05-11
my client has requested that a URL work regardless of whether or not there is a trailing slash

i've updated my htaccess file and my rewrites are getting this accomplished:

for example this:
http://ikonltd.com/current/
and this:
http://ikonltd.com/current
both go to the same page

here's my rewrite rule for that:

RewriteRule ^/current/([^\/\.]+)/?$ /current/index.cfm?PageNum_curreximages=$1 [QSA]

works great.

for some reason my rewrite for this URL is different:

http://ikonltd.com/new/

and this:
http://ikonltd.com/new

does not work.

here's my rewrite for that:

RewriteRule ^/new/([^\/\.]+)/?$ /newaq/index.cfm?PageNum_newaqimages=$1 [QSA]
RewriteRule ^/new/(.*) /newaq/$1 [QSA]

it has something to do with this last bit:
RewriteRule ^/new/(.*) /newaq/$1 [QSA] (which i'm not using anywhere else in my htaccess - just for the newaq section.

you'd think if i deleted it and just used this:
RewriteRule ^/new/([^\/\.]+)/?$ /newaq/index.cfm?PageNum_newaqimages=$1 [QSA]

it'd work but it does not.

again - i need both of these urls to resolve to the same page:
http://ikonltd.com/new/
http://ikonltd.com/new
0
Comment
Question by:phillystyle123
7 Comments
 
LVL 51

Assisted Solution

by:Steve Bink
Steve Bink earned 400 total points
ID: 35042919
The (.*) translates to "0 more more occurrences of any character".  So, "/new/" will match that rule.  As long as that rule comes after your optional trailing slash rule, it should still work as advertised.  If you need it to be first, though, modify it like this:

RewriteRule ^/new/(.+)$ /newaq/$1 [QSA]

Open in new window


As a side note, you are escaping characters inside the character set notation (i.e,. [^\/\.]).  That is unnecessary, except for non-printable characters (e.g., \n), and the backslash character itself.  It should not affect how the rule is implemented - just a minor technical note.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 35046467
"my client has requested that a URL work regardless of whether or not there is a trailing slash"

That is a bit of a problem.  

A URL without a trailing slash has a different meaning.  Without a trailing slash, the URL denotes a specific resource (a web page or script) something like http://example.com/index.html

When you have a trailing slash you are telling the web server to look for a directory, something like this (in the directory the server will try to find index.html or a similar index script):
http://example.com/images/

So what do you want to do if the client types this URL?  Should it try to run a script named images or locate the index of the images/ directory?
http://example.com/images

If you are convinced that the site you noted above has things just the way you want them, you might use this page and ask for a copy of their .htaccess file:
http://ikonltd.com/contact/

I do a somewhat similar thing on some of my sites, but all I do is look for a missing ".php" suffix.  If it's not there, and I get a 404, I try the URL with PHP appended.  So you might come to the site looking for "/results?game=5" and the server would locate "/results.php?game=5"

HTH, ~Ray
<?php // /404handler.php
error_reporting(E_ALL);

if (isset($_SERVER["REQUEST_URI"]))
{
    // IF A PHP SCRIPT IS NOT FOUND GO TO THE HOME PAGE OR SITE MAP
    if (preg_match('#\.php#i', $_SERVER["REQUEST_URI"]))
    {
        header('Location: /');
        exit;
    }

    // IF A NON-PHP SCRIPT IS NOT FOUND, TRY IT WITH PHP
    $arr = explode('?', $_SERVER["REQUEST_URI"]);
    $arr[0] .= '.php';
    $uri = implode('?', $arr);
    header("Location: $uri");
    exit;
}

// IF NO REQUEST URI
header('Location: /');
exit;

Open in new window

0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 200 total points
ID: 35046580
and this:
http://ikonltd.com/new

does not work.


Do you have:
DirectorySlash Off
Somewhere in the location?

If so, try doing
DirectorySlash On
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:phillystyle123
ID: 35060582
I definitely want the option to to go with or without the directory slash.

I'm not sure what "some-handler" is in this though:

RewriteRule ^/new/(.+?)/(.+?)/([^\/\.]+)/?$ /newaq/newacq_detail.cfm?permalink=$1&AutoArtID=$2&PageNum_newaqimages=$3 [QSA]
RewriteRule ^/new/(.+?)/([^\/\.]+)/?$ /newaq/popup.cfm?AutoArtID=$1&PageNum_newaqimages=$2 [QSA]
RewriteRule ^/new/([^\/\.]+)/?$ /newaq/index.cfm?PageNum_newaqimages=$1 [QSA]
RewriteRule ^/new/(.*)$ /newaq/$1 [QSA]
<Location /newaq/idex.cfm>
DirectorySlash Off
SetHandler some-handler
</Location>
0
 

Author Comment

by:phillystyle123
ID: 35075108
i managed to create a scenario where in new and new/ both resolve to the correct page - i just created an actual new directory on the server.

now, i need to adjust my .htaccess file so

http://ikonltd.com/new-editions  (no trailing slash)
resolves to the same page as
http://ikonltd.com/new-editions/ (trailing slash)

currently i just get a page not found when attempting to go to
http://ikonltd.com/new-editions  (no trailing slash)

my .htaccess code thus far:
RewriteRule ^/new-editions/(.+?)/(.+?)/([^\/\.]+)/?$ /new/newacq_detail-edition.cfm?permalink=$1&AutoArtID=$2&PageNum_newaqimages=$3 [QSA]
RewriteRule ^/new-editions/(.+?)/([^\/\.]+)/?$ /new/popup-edition.cfm?AutoArtID=$1&PageNum_newaqimages=$2 [QSA]
RewriteRule ^/new-editions/ /new/index-edition.cfm [QSA]
0
 

Accepted Solution

by:
phillystyle123 earned 0 total points
ID: 35085558
#Fix missing trailing slash char on folders
RewriteRule ^([^.?]+[^.?/])$ $1/ [R,L]
0
 

Author Closing Comment

by:phillystyle123
ID: 35126341
RewriteRule ^([^.?]+[^.?/])$ $1/ [R,L]

is the perfect solution for this problem
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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 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 …
Suggested Courses
Course of the Month18 days, 13 hours left to enroll

834 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