SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with MOD REWRITE (.htm and .php files)

    Hi,

    I am using this MOD REWRITE:

    Code:
    RewriteEngine On
    RewriteRule ^tutorials/([^/\.]+)/?$ tutorials.php?id=$1 [L]
    But it does not work.

    My site has mainly (about 10) .htm files. And 1 .php file.

    The problem is when the MOD REWRITE comes into effect if the user then clicks to go to any other page the MOD REWRITE causes a problem. It still thinks I want to MOD REWRITE when the user clicks to view a .htm file!

    What is going wrong?

    Matt.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,075
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    By itself, mod_rewrite doesn't check if files exist, unless you tell it to. Which you can do by using RewriteCond and %{REQUEST_FILENAME}, like so

    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^tutorials/([^/\.]+)/?$ tutorials.php?id=$1 [L]
    This means, if (RewriteCond) the requested URL (%{REQUEST_FILENAME}) is not an existing file (!-f), then apply the RewriteRule. Otherwise don't apply the RewriteRule.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is still there with your code.

    If the user clicks:

    tutorials/a/ there are really visiting tutorials.php?letter=a

    but if THEY NEXT click on contact.htm they are sent to:

    tutorials/a/contact.htm

    whereas I just want them to go to:

    contact.htm

    Why is the MOD REWRITE code not working?

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,075
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    That is not a mod_rewrite problem, that's a problem in your HTML.

    Instead of <a href="contact.html">contact</a> you must do <a href="/contact.html">contact</a>
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks - yes that it working now. Can you help with one further thing please?

    How do I add .htm to the end of the URL that appears online?

    currently I have "tutorials/a" as the URL I enter into my pages and then the MOD REWRITE does its work in the background (as you know).

    If I write tutorials/a.htm then it will be trying to load tutorials.php?letter=a.htm which would be wrong.

    Any ideas?

    Matt.

  6. #6
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have just noticed the code has caused a problem:

    In this code in the page:

    Code:
    if(preg_match('#^[a-z]{1}$#i', $_GET['letter'])) {
        $letter = strtolower($_GET['letter']); // valid input
    }else{
        $letter = 'a'; // default value for invalid input
    }
    it is always choosing the letter 'a' (for an invalid input)

    Has the MOD REWRITE caused a problem with the GET part of my coding??

    Matt.

  7. #7
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have solved the problem. But I still keen to get a the link with .htm after it, if you know how?

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,075
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Sure, just change it to

    Code:
     
    RewriteRule ^tutorials/([^\.]+)\.htm$ tutorials.php?id=$1 [L]
    Or even better, since you just want to accept a through z:

    Code:
     
    RewriteRule ^tutorials/([a-z])\.htm$ tutorials.php?id=$1 [L]
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks - I've used the A-Z one.

  10. #10
    SitePoint Member songspk's Avatar
    Join Date
    Feb 2014
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if there is number in url then it could be
    Code:
    RewriteRule ^tutorials/([a-z][0-9])\.htm$ tutorials.php?id=$1 [L]

  11. #11
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,075
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by songspk View Post
    if there is number in url then it could be
    Code:
    RewriteRule ^tutorials/([a-z][0-9])\.htm$ tutorials.php?id=$1 [L]
    That would match any letter plus a digit, like a1, b2, c9, d4, etc.
    If you'd like to match any letter or digit (which is not the case here I don't think), you'd use

    Code:
    RewriteRule ^tutorials/([a-z0-9])\.htm$ tutorials.php?id=$1 [L]
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •