SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    Jem
    SitePoint Community Guest

    Learn Apache mod_rewrite: 13 Real-world Examples

    Notice: This is a discussion thread for comments about the SitePoint article, Learn Apache mod_rewrite: 13 Real-world Examples.
    __________

    I know it's a tiny, inconsequential thing... but under the "forbidden|F" section of "mod_rewrite Flags", in the title text of "bandwidth", "date" should read "data".

    (Thanks, Anal-Retentive Man! You've saved SitePoint once again!)

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    Athens, Greece
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice tutorial - thank you!

  3. #3
    Bobo Linq
    SitePoint Community Guest
    You write:

    <blockquote>If you've shifted files around on your site, changing directory names, try this:

    RewriteRule ^/?old_directory/([a-z/.]+)$ new_directory/$1 [R=301,L]

    I've included the literal dot character (not the "any character" metacharacter) inside the set to allow file extensions. </blockquote>

    I think you wrote "/." when you meant "\." — you have not escaped the dot character as you say.

  4. #4
    Anonymous
    SitePoint Community Guest
    This passage is a little unclear:
    <blockquote>The {REQUEST_URI} string starts with a / character. Apache changed regex engines when it changed versions, so Apache version 1 requires the leading slash while Apache 2 forbids it!</blockquote>
    Are you saying that Apache 1 and 2 each store {REQUEST_URI} differently? Are you saying that although they both store {REQUEST_URI} the same (with a leading slash), Apache 2 directs you to omit leading slashes when writing a pattern rule to match against {REQUEST_URI} because it takes the leading slash for granted? Something else?

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

    Apache 1.x: ^/ is required in the DocumentRoot

    Apache 2.x: ^ (the leading / will PREVENT a match) is required in DocumentRoot

    The two regex engines obviously do things differently (with the {REQUEST_URI} string) so your answer is "yes."

    On the other hand, I believe that http://domain.com%{REQUEST_URI} would be valid in both versions. Now, that's what I call confusing!

    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
    Delos
    SitePoint Community Guest
    Great article.

    I have a site where the urls are www(dot)domain(dot)com/article-title(dot)html. I would like to set up the htaccess file to have users be able to type www(dot)domain(dot)com/article-title and be directed to the html file. I imagine there should also be a default rule that directs them to a zero-results url, if possible.

    It seems like it should be like number 8 but I'm not sure how to code the cond/rewrite rules. Can you assist?

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Bobo Linq View Post
    You write:

    <blockquote>If you've shifted files around on your site, changing directory names, try this:

    RewriteRule ^/?old_directory/([a-z/.]+)$ new_directory/$1 [R=301,L]

    I've included the literal dot character (not the "any character" metacharacter) inside the set to allow file extensions. </blockquote>

    I think you wrote "/." when you meant "\." you have not escaped the dot character as you say.
    Bobo,

    Good catch but not correct! When INSIDE a range definition like that, the dot character does NOT get escaped so I was allowing both the / and the dot along with lowercase characters.
    Quote Originally Posted by Delos
    I have a site where the urls are www(dot)domain(dot)com/article-title(dot)html. I would like to set up the htaccess file to have users be able to type www(dot)domain(dot)com/article-title and be directed to the html file. I imagine there should also be a default rule that directs them to a zero-results url, if possible.
    Delos,

    That's quite simple - like adding the php extension to an extensionless filename (didn't I have that in the article - or in the article on my site which is linked in my signature?). Here goes ... just for you!
    Code:
    RewriteEngine on
    RewriteRule ^([a-z]+)$ $1.html [L]
    This only allows lowercase characters (do NOT add the dot character like I did in Bobo's example as it would then match whatever.html) and will IGNORE any domain-only request (the + requires one or more lowercase characters). If you need uppercase, change [a-z]+ to [a-zA-Z]+ and other characters (EXCEPT {space} or RESERVED characters) as required. Post in the Apache forum for more help, if you please.

    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

  8. #8
    long
    SitePoint Community Guest
    13. Enforcing secure server only on selected pages => it doesn't work, I tried several times and nothing

    BTW: not "RewriteCond %{ SERVER_PORT } ^443$"
    but: "RewriteCond %{SERVER_PORT} ^443$"

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

    Thanks! I hadn't seen that. Indeed, there are no spaces in an Apache variable!

    What's happening with the request? Did you specify each of your pages within the (x|y|z) atom?

    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
    M Hussain
    SitePoint Community Guest
    Nice Tutorial. It helps me a lot.

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

    Thanks!

    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

  12. #12
    SitePoint Enthusiast
    Join Date
    Oct 2009
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    8. Creating extensionless links

    If your site uses PHP files, and you want to make your links easier to remember -- or you just want to hide the file extension, try this:

    RewriteRule ^/?([a-z]+)$ $1.php [L]

    If you have a mixture of both .html and .php files, you can use RewriteCond statements to check whether the filename with either extension exists as a file:

    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^/?([a-zA-Z0-9]+)$ $1.php [L]
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^/?([a-zA-Z0-9]+)$ $1.html [L]

    If the file name exists with the .php extension, that rule will be chosen.

    My code is:

    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^http://forum.movieat.co.cc/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://forum.movieat.co.cc$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://movieat.co.cc/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://movieat.co.cc$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.forum.movieat.co.cc/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.forum.movieat.co.cc$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.movieat.co.cc/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.movieat.co.cc$ [NC]
    RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://movieat.co.cc/ [R,NC]
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^/?([a-zA-Z0-9]+)$ $1.html [L]
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^/?([a-zA-Z0-9]+)$ $1.php [L]

    It does not work!

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

    It looks like you've just repeated a coding example from the article then added a list of referrers to PREVENT a redirect.
    Code:
    RewriteEngine on
    # If NOT any of these domains ...
    # BTW, you DON'T need and DON'T want the end anchors
    RewriteCond &#37;{HTTP_REFERER} !^http://forum.movieat.co.cc/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://forum.movieat.co.cc$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://movieat.co.cc/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://movieat.co.cc$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.forum.movieat.co.cc/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.forum.movieat.co.cc$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.movieat.co.cc/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.movieat.co.cc$      [NC]
    RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://movieat.co.cc/ [R,NC]
    # all the above is ANDed with ...
    RewriteCond %{REQUEST_FILENAME}.html -f  
    RewriteRule ^/?([a-zA-Z0-9]+)$ $1.html [L]
    # The Last flag eliminates the domain exclusion from:
    RewriteCond %{REQUEST_FILENAME}.php -f  
    RewriteRule ^/?([a-zA-Z0-9]+)$ $1.php [L]
    Well, what's your TEST URL? That makes a difference if you need someone to troubleshoot this for you.

    BTW, if you have questions about mod_rewrite, they should be posted in the Apache forum. I didn't move your post (and my response) as you'd not be likely to find it.

    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

  14. #14
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how do i build a database?


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
  •