Solved

htaccess rewrite rule - optional trailing slash

Posted on 2011-03-04
7
902 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 50

Assisted Solution

by:Steve Bink
Steve Bink earned 100 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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 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 50 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Re-imbursement Claim System 3 25
html input clean up 3 32
Filter wordpress query 3 13
showing numeric numbers 2 11
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 …

746 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

12 Experts available now in Live!

Get 1:1 Help Now