Solved

htaccess rewrite rule - optional trailing slash

Posted on 2011-03-04
7
906 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 109

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
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.

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP strings vs array errors 13 33
Decrypt string by php 7 47
How to fix Datetime in MySQL? 4 49
Wordpress Pagination 1 29
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

838 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