SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Add trailing slash to rewritten location

    I have this code in my .htaccess file

    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^home/?$ /home.php [NC]
    RewriteRule ^settings/?$ /settings.php [NC]
    RewriteRule ^verify/([0-9]+)/([0-9]+)/?$ /verify.php?id=$1&user=$2 [NC,L]
    The question mark after the "/" in the rules make something like domain.com/home AND domain.com/home/ work.

    I want to only have domain.com/home/ work. But if someone goes to domain.com/home, I want it to redirect to the one with the slash.

    I realize I can create another set of conditions for each of the above and redirect any that doesn't have a "/" to the correct one, but is there a catch-all for this?

    I tried the following, but it ended up redirecting me to domain.com/home.php/

    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !\..+$
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^/(.*)$ http://domain.com/$1/ [L,R=301]

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Mr. PITA,

    First, allow me to comment on your mod_rewrite code so others will know why it cannot work:
    Code:
    RewriteEngine On
    # set mod_rewrite in action (NOT comment) mode)
    RewriteCond %{REQUEST_FILENAME} !-f
    # request is not a file
    RewriteCond %{REQUEST_FILENAME} !-d
    # request is not a directory
    RewriteRule ^home/?$ /home.php [NC]
    # request starts with home or home/ 
    # (or any silly case with CAPS and lower mixed -
    # BAD use in a RewriteRule (designed for {HTTP_HOST})!
    # AND
    RewriteRule ^settings/?$ /settings.php [NC]
    # same problem with case of the {REQUEST_URI} string
    # but can NEVER match because it's been ANDed with the
    # home RewriteRule above
    RewriteRule ^verify/([0-9]+)/([0-9]+)/?$ /verify.php?id=$1&user=$2 [NC,L]
    # same problem with the case of the {REQUEST_URI} string
    # but can NEVER match because it's been ANDed with both
    # the home and settings RewriteRules
    Quote Originally Posted by Mr PITA
    The question mark after the "/" in the rules make something like domain.com/home AND domain.com/home/ work.
    Of course it does. HOWEVER, the resulting home.php (the only possible redirection) will not know which directory level it's in so absolute links are required for it to function properly (support files, e.g., images, css, js, et al).
    Quote Originally Posted by Mr PITA
    I want to only have domain.com/home/ work. But if someone goes to domain.com/home, I want it to redirect to the one with the slash.

    I realize I can create another set of conditions for each of the above and redirect any that doesn't have a "/" to the correct one, but is there a catch-all for this?
    I've already addressed the WHY you do NOT want to do that. If you insist, though, you've got to add another mod_rewrite block statement to force the trailing /. With the existing problems, though, I don't think it's worthwhile going through your ending code with all the EVERYTHING atoms.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code I have works fine though.

    domain.com/home shows home.php
    domain.com/settings shows settings.php
    etc

    I want to add the trailing slash to the urls for consistency's sake and so as to not have duplicate links and be penalized in search listings

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by mr.pita View Post
    The code I have works fine though.

    domain.com/home shows home.php
    domain.com/settings shows settings.php
    etc

    I want to add the trailing slash to the urls for consistency's sake and so as to not have duplicate links and be penalized in search listings
    Mr. PITA,

    The code you displayed above DOES have the problems noted so, if your code "works fine," you've not displayed your code.

    Did you even read WHY you should NOT use the trailing /? If you insist, then please show your REAL code so we can work through the obvious problems to get you to URI/.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's pretty much all my code. There's some extra but I don't think it should change anything.
    As for the trailing url, I have been using absolute links in my html. If you think it's better to go without, then I'd like to do the opposite and redirect all rewritten urls with a trailing slash, to urls without it. I just would like it to be consistent because I will need to rely on SEO a lot for this project.

    Here's a screenshot of everything:


    I know very little about working with .htaccess files but this stuff seems to serve my purposes so far.

    I really appreciate your help

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Mr PITA,

    Aw, you KNOW you get my biases here as much as you get comments which are supposed to help you as well as every other member who wander into your thread!

    My bias against using a trailing / is based on:

    1. It just looks ugly.

    2. It just looks like a directory (despite any file extension).

    3. It add an unnecessary character to the URI.

    4. It adds work for you (the webmaster) in creating absolute links in your scripts. Others may not see that and wonder why I get upset with the trailing / until they have problems with relative links. If you think that it's bad standardizing with trailing slashes, wait until you hear the problems members have with OPTIONAL trailing slashes! They MUST use absolute URLs for their links as neither Apache nor their browsers can figure out what directory level the visitor has requested.

    For all my whining, though, it's YOUR website you're dealing with and I'm here to help you understand what your mod_rewrite code is doing (and how to make it do what you want it do to). Moreover, if you think I whine about trailing slashes, look around at the way that I ABUSE people for using "lazy regex," i.e., the EVERYTHING atom without understanding what it's doing for/TO them. Okay, I'm turning off my RANT mode (finally) and getting back to your problem.

    Consistent good, random bad.

    Your code, however, does suffer from ... ARGH! An image of your code? PLEASE post the code (wrapped in the code tag, not the quote tag) so I can comment IN the code for better understanding!

    I'd commented on your code before but your image shows the same problems, i.e.,

    1. ONE HUGE block statement, i.e., your pair of RewriteCond statements are ANDed with each and every RewriteRule - and those are mutually exclusive so you can never match the second (or successive) RewriteRule. You appear to KNOW about the Last flag (the last RewriteRule has one) but you are apparently not aware that it MUST be used for every mod_rewrite block statement. Think of it in terms of PHP's } terminating a block statement (opened with a {). PHP will not compile without the terminating } but mod_rewrite assumes that you want to AND the output of a non-terminated RewriteRule with the next (block statement) RewriteRule.

    2. The No Case flag is designed for use on the {HTTP_HOST} variable which is NOT case sensitive (and the regex engine IS). It is clearly NOT acceptable to tell mod_rewrite to match SiGnIn, is it? Okay, it may be IF you're not using any of the regex in the redirection (back references) but that means that you shouldn't be using mod_rewrite - mod_alias's Redirect is better suited to simple match and go redirects.

    With the evident lack of knowledge, may I ask that you read the mod_rewrite tutorial linked in my signature. It HAS helped many members as it has been written as an accumulation of years of questions and answers here in this forum. I'll bet you understand ALL about it when you're finished!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I'll take a look at your mod_rewrite tutorial.

    But one question before I do that, if I add a Last flag to each rule, then how does the server know when the condition doesn't apply to the rules anymore? Or do I have to repeat the conditions before each rule? That just seems like a lot of extra stuff.

    Thanks so much!

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Mr. PITA,
    Quote Originally Posted by mr.pita View Post
    Ok, I'll take a look at your mod_rewrite tutorial.

    But one question before I do that, if I add a Last flag to each rule, then how does the server know when the condition doesn't apply to the rules anymore? Or do I have to repeat the conditions before each rule? That just seems like a lot of extra stuff.

    Thanks so much!
    Like PHP and JavaScript, the block statements are complete statements unto themselves, i.e., the RewriteCond statements are ONLY associated with the block in which they reside. Therefore, they need to be appended to each block statement to which they apply (since they don't apply, which these do not - they're basically all Redirects, you don't need them in any block statement).

    Alternatively, you could also use the Skip flag to skip these mod_rewrite blocks if the conditions are NOT met. The Skip flag acts like a GOTO around the (S=x) next x RewriteRules. Yeah, this OLD DOG learned that trick just this year!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  9. #9
    SitePoint Addict mr.pita's Avatar
    Join Date
    Aug 2005
    Location
    USA
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, right--the skip flag!
    Thanks for all your help DK!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •