Solved

htaccess rewrite rule - optional trailing slash

Posted on 2011-03-04
7
910 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
[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
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 110

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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

749 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