Results 1 to 15 of 15

Thread: [htaccess] Remove query string?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    65

    [htaccess] Remove query string?

    Sorry if I posted in the wrong forum I couldnt really find one, anyway.

    I am trying to remove query strings accorss the whole site and replace them with a forward slash and if possible remove the "&" and "=" replace that with with a forward slash, now here is my current rewrite rule to remove .php extension.

    I am extremely new to .htaccess and I have 0 knowledge about it, so any help would be appreciated.

    Code:
    Options +FollowSymLinks
    Options -Multiviews
    RewriteEngine On
    RewriteBase /
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]

  2. #2
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: [htaccess] Remove query string?

    I'm not an expert on Apache configs, so don't expect too much from this.

    Check the configs for RewriteRule at http://httpd.apache.org/docs/current...ml#rewriterule, specifically the info box with the heading "Modifying the Query String".

    By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    65

    Re: [htaccess] Remove query string?

    Thanks for the reply,

    I cant make much out of that sorry, I am pretty new to this sort of thing, not sure how much of this works.

  4. #4
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: [htaccess] Remove query string?

    You only need the "RewriteEngine On" line once.

    So... have you tried the htaccess file as you've posted it? If so, is it working as you'd expected, or not? What problems are you having with it? I don't see anything immediately wrong with it, but it depends on what you're trying to achieve.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    65

    Re: [htaccess] Remove query string?

    Yes it seems to be working, however I have several things I want to remove, like for example non rewritted url would be: url/members.php?pid=1 and I want that to appear like: url/members/2

    at the moment its displaying it as: url/members/?pid=2

    Thanks for any help.

  6. #6
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: [htaccess] Remove query string?

    htaccess can't discern what part of your "pretty" URL (url/members/2) is a directory path, a file name, or a query string var. So you'd probably need to write some specific rules; for instance:
    Code:
    RewriteRule ^url/members/([0-9]*)?$ /url/members.php?pid=$1 [NC]
    You can create a hierarchy of specific rules followed by a generic one:
    Code:
    RewriteRule ^url/members/([0-9]*)?$ /url/members.php?pid=$1 [NC,L]
    RewriteRule ^url/other/([a-z]*)?$ /url/other.php?var=$1 [NC,L]
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    This won't scale very well though: you'll essentially need a new rule for every URL that could have a query string. If your site is small, that may not be too bad.
    Last edited by SambaNeko; Aug 9th, 2011 at 05:19 PM. Reason: Adding L flags where needed for an accurate example.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    65

    Re: [htaccess] Remove query string?

    Thats perfect theres not many query strings across the whole website, also url bit of the path was the website url.

    Thanks!

    EDIT: Also to allow alphanumeric in the ?pid= do I just change

    RewriteRule ^members/([0-9]*)?$ /members.php?pid=$1 [NC]
    To

    RewriteRule ^members/([0-9][a-z]*)?$ /members.php?pid=$1 [NC]
    So like it would be like this: www.website.com/members/Username123
    Last edited by MrTree; Aug 8th, 2011 at 09:23 PM.

  8. #8
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: [htaccess] Remove query string?

    To allow alphanumeric, put the ranges within the same set of square braces:
    Code:
    RewriteRule ^members/([0-9A-Za-z]*)?$ /members.php?pid=$1 [NC]
    (added the uppercase character range too)

  9. #9
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [htaccess] Remove query string?

    Quote Originally Posted by SambaNeko View Post
    To allow alphanumeric, put the ranges within the same set of square braces:
    Code:
    RewriteRule ^members/([0-9A-Za-z]*)?$ /members.php?pid=$1 [NC]
    (added the uppercase character range too)
    The [NC] tag means "No Case," so you can specify either the uppercase or lowercase ranges and not have a problem.
    Like Archer? Check out some Sterling Archer quotes.

  10. #10
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: [htaccess] Remove query string?

    Yeah, I knew what NC stood for, but I always see examples where people just do both ("A-Za-z" and [NC]), and was too lazy to research or test if there was a reason for that. I suppose there isn't.

    But why not cover all of your bases in case the official flag reader has the day off? /s

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    65

    Re: [htaccess] Remove query string?

    Thanks guys, will try this once I get back home.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    65

    Re: [htaccess] Remove query string?

    Sorry guys aint working im getting "No input file specified. ". When typing this: website.com/members/username it redirects to website.com//members.php/username/?pid=username

    Also this is the updated .htaccess
    <files .htaccess>
    order allow,deny
    deny from all
    </files>

    Options +FollowSymLinks
    Options -Multiviews
    RewriteEngine On
    RewriteBase /

    #Remove .php from urls
    RewriteCond &#37;{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^admin.*
    RewriteCond %{REQUEST_URI} !^/app.*
    RewriteCond %{REQUEST_URI} !^/dl.*
    RewriteRule ^([^/]+)/$ $1.php
    RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php

    #Remove ?pid= from members url
    RewriteRule ^members/([0-9A-Za-z]*)?$ /members.php?pid=$1 [NC]

    #Add slash at end of url.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^admin.*
    RewriteCond %{REQUEST_URI} !^/app.*
    RewriteCond %{REQUEST_URI} !^/dl.*
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ /$1/ [R=301,L]
    EDIT: Also another question to add a trailing slash its this right?
    RewriteRule ^members/([0-9A-Za-z]*)?$/ /members.php?pid=$1 [NC]
    Last edited by MrTree; Aug 9th, 2011 at 04:42 PM.

  13. #13
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: [htaccess] Remove query string?

    Forgot to mention in a prior post, add the L ("last") flag to have htaccess stop rewriting after a rule:
    Code:
    #Remove ?pid= from members url
    RewriteRule ^members/([0-9A-Za-z]*)?$ /members.php?pid=$1 [NC,L]
    And then move this rule above the other two blocks.

    To add a slash to the matching pattern, put it before the $:
    Code:
    RewriteRule ^members/([0-9A-Za-z]*)?/$ /members.php?pid=$1 [NC]
    The $ in this pattern signifies the end of the URL. Be aware that this makes the / a requirement for matching the pattern; so "members/username/" would match, but "members/username" would not. If you want to make it optional (so that either would match), add a ?:
    Code:
    RewriteRule ^members/([0-9A-Za-z]*)?/?$ /members.php?pid=$1 [NC]

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    65

    Re: [htaccess] Remove query string?

    Thank you very much! works like a charm!

    Ok one last thing I promise

    I have a link like this:
    website.com/members/?page=Stuff&mode=other
    Thanks to your help ive got it to:
    website.com/members/Stuff&mode=other
    However the &mode=other doesent seem to work, how would I split it up to something like:

    website.com/members/Stuff/other/
    If not too difficult.

    Thanks for everything.
    Last edited by MrTree; Aug 9th, 2011 at 05:58 PM.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    65

    Re: [htaccess] Remove query string?

    Never mind I have fixed it.

    Sorry for double post cant edit last one.

Posting Permissions

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



Click Here to Expand Forum to Full Width