SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Steve Benjamins tnrstudios's Avatar
    Join Date
    Jun 2003
    Location
    canada
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with Re-Write URL

    I am trying to use .htaccess to rewrite my urls and I am completely stuck.

    I am trying to rewrite:
    winks.php?cat=16
    to:
    winks16.htm

    using this htaccess:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule winks(.*)\.htm$ /winks.php?cat=$1

    Any idea's where I'm going wrong?
    Or could it be an issue with the server?

  2. #2
    SitePoint Guru
    Join Date
    May 2004
    Location
    santa rosa, ca
    Posts
    969
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule winks(.*)\.htm$ /winks.php?cat=$1

    That is exactly right. Ask your host to make sure the The RewriteEngine directive is enabled.
    nondenominational, noncommercial, nonprofit,
    listener-supported, 24-hour, Christian ministry:
    Listen Live Online

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,692
    Mentioned
    20 Post(s)
    Tagged
    3 Thread(s)
    tnr,

    Actually, you're going the other way with the redirection - but you knew that from the code.

    Personally, I AVOID the EVERYTHING atom like the plague (it has roughly the same effect if you don't know what you're doing). Instead of (.*), may I suggest ([0-9]+) to require one or more DIGITS. Also, the leading slash in the redirect may cause problems - it's not needed.

    Finally, Santa is correct - except that your httpd.conf should already have Options All so the first line isn't needed. If your host hadn't enabled mod_rewrite (see my signature article for a quick test) then it won't work no matter what you do (except to find a decent hosting company).

    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

  4. #4
    Steve Benjamins tnrstudios's Avatar
    Join Date
    Jun 2003
    Location
    canada
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tested mod-rewrite using your signature tutorial and it worked perfectly. So I dunno, if santas script is perfect then something is really messed up on my server I guess.

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,692
    Mentioned
    20 Post(s)
    Tagged
    3 Thread(s)
    trn,

    All you really need in your .htaccess (for this redirect is):
    Code:
    RewriteRule ^/?winks([0-9]+)\.htm$ winks.php?cat=$1 [L]
    Give that a try and see if that doesn't do the trick for you.

    First, URIs begin with a leading slash (unless internally redirected ...).
    Second, learn to specify EXACTLY what you will allow in your regex.
    Third, the leading slash in the redirect isn't required (IF it's in the same directory).
    Fourth, the Last flag terminates this "block" statement.

    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

  6. #6
    SitePoint Guru LinhGB's Avatar
    Join Date
    Apr 2004
    Location
    Melbourne, Australia
    Posts
    902
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's best to avoid ^/ in .htaccess (or RewriteRules at directory level) IMO as you'll get confused with the directory path. This should be sufficient:

    RewriteRule ^winks([0-9]+)\.htm$ winks.php?cat=$1 [L]

    When using URL rewriting in a directory or .htaccess context, you are making the assumption that the URL is related to the physical filename path (50% of the time it isn't), so in case it is not, you must use RewriteBase otherwise it won't work:

    Quote Originally Posted by Apache docs
    If your webserver's URLs are not directly related to physical file paths, you have to use RewriteBase in every .htaccess files where you want to use RewriteRule directives.
    If the previous RewriteRule by itself doesn't work for you then insert RewriteBase.

    Btw rewriting URL at .htaccess level has a bad performance hit. I always do Rewrites in apache configs. Sometimes you have no choice but .htaccess though.
    "I disapprove of what I say,
    but I will defend to the death my right to say it."

  7. #7
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    512
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've already explained this many times to David and others.

    In short, in the .htaccess, the directory-prefix is removed before matching.
    So, there is no need for the first /.

    However, if someone (or badly written robots) make a request like http://example.com////subfir/file.html
    the rule wouldn't work, and it might present security risks.

    That's why I often use ^/*Regex to abosrob possible extra slashes.

    Also, this makes the rule compatible in both .htaccess and httpd.conf, in most cases.
    As many people don't provide enough info when they ask, and we don't know if the rule is used in .htaccess or httpd.conf, suggesting a code that works for both contexts is generally better, I think.


    More explanation.

    In .htaccess (or inside <directory> of httpd.conf),
    The Regex of RewriteRuke is matched against REQUEST_URI
    after decoding URL escaped charaqters and removing directory prefix.

    It's explaned in the documents, and we can see it by using RewriteLog on with a high RewriteLovLevel.

    Example1:
    Code:
    #.htaccess of docroot:
    RewriteBase /
    RewriteRule ^(.*)$ printenv.cgi?uri=$1 [L,QSA]
    
    URL1: http://example.com/subdir/file.html
    REQUEST_URI == /subdir/file.html
    decoding URL escape == /subdir/file.html
    directory prefix == /  (Because the rule is in the .htaccess of docroot / )
    ==> (.*) will be matched against subdir/file.html
    New REQUEST_URI == /printenv.cgi
    New QUERY_STRING == subdir/file.html
    
    URL2: http://example.com/subdir/file%22xyz.php?if=123
    REQUEST_URI == /subdir/file%22xyz.php (In the RewriteRule, QUERY_STRING is removed)
    decoding URL escape == /subdir/file"xyz.php
    directory prefix == / (Because the rule is in the .htaccess of docroot / )
    ==> (.*) will be matched against subdir/file"xyz.php
    New REQUEST_URI == /printenv.cgi
    New QUERY_STRING == id=subdir/file%22xyz.php&id=123 (Unless there is [NE] flag, it's encoded back)

    Example2:
    Code:
    #.htaccess of /subdir:
    RewriteBase /
    RewriteRule ^(.*)$ printenv.cgi?uri=$1 [L,QSA]
    
    URL1: http://example.com/subdir/file.html
    REQUEST_URI == /subdir/file.html
    decoding URL escape == /subdir/file.html
    directory prefix == /subdir/  (Because the rule is in the .htaccess of /subdir/ )
    ==> (.*) will be matched against file.html
    New REQUEST_URI == /printenv.cgi
    New QUERY_STRING == file.html
    
    URL2: http://example.com/subdir/file%22xyz.php?if=123
    REQUEST_URI == file%22xyz.php  (In the RewriteRule, QUERY_STRING is removed)
    decoding URL escape == /subdir/file"xyz.php
    directory prefix == /subdir/ (Because the rule is in the .htaccess of /subdir/ )
    ==> (.*) will be matched against file"xyz.php
    New REQUEST_URI == /printenv.cgi
    New QUERY_STRING == id=file%22xyz.php&id=123 (Unless there is [NE] flag, it's encoded back)

    While ^/? which is prefered by Daivid provides the compatibility for the both contexts,
    it doesn't cover a URL with many slashes.
    And it doesn't make sense to match just zero or one slash when we can match
    many or zero slash and that often gives us more security.


    Anyone willing to learn mod_rewrite SHOULD use RewriteLog to understand all these, IMHO.
    Otherwise, we can keep guessing and confuse ourselves and others.


    PS.

    I agree with the use of RewriteBase, and performance issue raised by LinhGB.

    The use of RewriteBase may contribute to reduce infinite looping, too.
    (In case of RewriteRules used in the .htaccess)
    This will go into the loop and causes internal server error.
    RewriteRule ^ /index.cgi [L]

    This won't.
    RewriteBase /
    RewriteRule ^ index.cgi [L]

    But it's better to write like this.
    RewriteBase /
    RewriteRule !^/*index.cgi index.cgi [L]
    Last edited by extras; Jan 23, 2006 at 08:48.
    Freebie/DonationWare: check-these.info
    Custom solutions: Hostwick.com

  8. #8
    Steve Benjamins tnrstudios's Avatar
    Join Date
    Jun 2003
    Location
    canada
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    guys the weird thing is none of these are working for my website...any ideas why?

  9. #9
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,692
    Mentioned
    20 Post(s)
    Tagged
    3 Thread(s)
    tnr,

    There are a couple of possible causes:

    1. The URLs you're using are not what you've described to us. In other words, you'll need to show us the exact URLs you're using

    2. There are other (higher directory or httpd.conf) directives in play BEFORE these are actioned. This will require a review of your httpd.conf and all .htaccess files from the DocumentRoot to the .htaccess you're playing with.

    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

  10. #10
    Steve Benjamins tnrstudios's Avatar
    Join Date
    Jun 2003
    Location
    canada
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOW!

    I apologize, I finally found the problem...I had mispelled htaccess....

  11. #11
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,692
    Mentioned
    20 Post(s)
    Tagged
    3 Thread(s)
    tnr,

    It's always those small things that get overlooked. Glad that YOU got it sorted out!



    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


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
  •