SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot rhlowe's Avatar
    Join Date
    Sep 2006
    Location
    San Diego
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unsuccessfully trying to rewrite a url [mod_rewrite]

    Hi folks, I am being vexed by and issue trying to use the rewrite properties in my .htaccess file.

    I am trying to take a url as such: mysite.com/index.php?q=something/like/this
    and make it this: mysite.com/something/like/this

    I dont want to redirect to that directory as it doesn't exist, I just want the url to read that way.

    The whole of my .htaccess file reads:

    Code:
    RewriteEngine on
    RewriteRule ^index.php?q=([a-z/?]+)$ $1 [L]
    I am a level 1 .htaccess novice and have read every resource in the sticky .htaccess thread plus more stuff I have found around the web and just cannot make it work, can anyone offer advice?
    San Diego Freelance Web Development Contractor
    Great Taste, no Filler. Now with no Trans Fat!

  2. #2
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    RewriteEngine on
    RewriteRule ^/index\.php\?q=([a-z/]+)$ /$1 [L]
    . means any character, ? means optional character.
    So the ^index.php?q=([a-z/?]+)$ matches index.phq=a and index.phpq=a.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

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

    It's obvious that you're trying to match a query string with your regex. Since you haven't read the tutorial article linked in my signature, you wouldn't know that. Do it and come back for answers (IF you still have questions).

    Alex,

    I believe that = is a reserved character (to be used ONLY in a query string) so even your explanation falls apart for that as well as trying to match the query string by trying to escape the ? (which is ONLY allowed in a URL as the divider character between the URI and query string).

    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
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rhlowe, sorry - my solution is incorrect.
    DK, thank you for correcting me.

    Am I right that RewriteRule Pattern Substitution applies the pattern only on the path part of the URL, e.g. it applies it only on /path/mypage.php in http://host/path/mypage.php?q=1? So there is no way to apply RewriteRule on the complete URL?
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

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

    Sorry, I missed this thread!

    RewriteRule ONLY accesses the URI (after the domain - difference between Apache 1 & 2 about "ownership" of the /) up to but NOT including the ? which designates the end of the URI and the {QUERY_STRING}.

    RewriteCond allows a webmaster to access other Apache variables, i.e., {HTTP_HOST}, {QUERY_STRING}, {REMOTE_ADDR}, {HTTPS}, etc and is VERY useful as it is then ANDed (by default - OR is available via the OR flag) with the following RewriteRule.

    Have a look at my signature's tutorial article where there are many useful examples (it's amazing what others have wanted to do!) from a few years in the Apache Forum.

    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 Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dklynn,

    Thank you for the comment and article. Very useful.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  7. #7
    SitePoint Zealot rhlowe's Avatar
    Join Date
    Sep 2006
    Location
    San Diego
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems I have my code backwards, when I changed it to this:
    Code:
    RewriteEngine on
    RewriteBase /schools
    RewriteRule ^([a-z/]+)$ index.php?q=$1 [L]
    now going to site.com/schools/something treats the url as site.com/schools/index.php?q=something

    Thanks for all the help!!
    San Diego Freelance Web Development Contractor
    Great Taste, no Filler. Now with no Trans Fat!


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
  •