www.webdeveloper.com
Results 1 to 12 of 12

Thread: .htaccess - Escaping ? (Question Mark)

  1. #1
    Join Date
    Jun 2008
    Location
    Europe
    Posts
    1,086

    .htaccess - Escaping ? (Question Mark)

    I am trying to rewrite my URLs so that they do not contain question marks...

    EG - I would like to turn this:
    Code:
    http://www.MY-URL.com/products.php?product-x
    Into this:
    Code:
    http://www.MY-URL.com/products/product-x
    This will not work:
    Code:
    RewriteRule ^/products/ /products.php?
    I think the solution might have something to do with using %{QUERY_STRING} as a question mark is not considered part of an URL.

  2. #2
    Join Date
    Sep 2008
    Location
    Mexico
    Posts
    786
    IF products.php?product-77a
    is the repeating format of the URI.... using the word "product" for each item...
    Code:
    RewriteRule ^products/product\-([^/]) /products.php?product\-$1 [NC,L]
    should result in /products/product-77a


    IF it is something like products.php?shoes-66x, products.php?shirts-21b
    Code:
    RewriteRule ^products/([^/]+)\-([^/]) /products.php?$1\-$2 [NC,L]
    should result in /products/shoes-66x and /products/shirts-21b

    .... but only if I've already had enough coffee this morning!
    ...but stupidity is terminal.

  3. #3
    Join Date
    Jun 2008
    Location
    Europe
    Posts
    1,086
    Thanks for the reply Opifex.

    Tried both of your suggestions and I'm getting the same result as with other attempts...

    For some reason, the "?" is not showing up, so I'm getting the file I want, but without the query results. If I manually add the "?" to the URL, the page loads fine.

    If anyone wants to see the barebones functionality (I'm experimenting with it as I write this...) you can see the query by itself here:

    http://www.tank-tops.net/products.php?

    Any word you type after the question mark will return products which correspond...
    For example:
    http://www.tank-tops.net/products.php?dogs

    I've got it working with .htaccess like this:
    http://www.tank-tops.net/?dogs-sweatshirts (etc.)

    BUT... cannot get rid of that pesky question mark!

  4. #4
    Join Date
    Sep 2008
    Location
    Mexico
    Posts
    786
    Shouldn't have to escape the "?".... but, try
    Code:
    RewriteRule ^products/([^/]+)\-([^/]) /products.php\?$1\-$2 [NC,L]
    and see if it makes any difference. Curious.
    ...but stupidity is terminal.

  5. #5
    Join Date
    Sep 2008
    Location
    Mexico
    Posts
    786
    Was playing with the addresses....
    give this a try also
    Code:
    RewriteRule ^products/([^/]+)\-([^/]) /products.php?product=$1\-$2 [NC,L]
    because http://www.tank-tops.net/products?product=dogs-tshirt works
    ...but stupidity is terminal.

  6. #6
    Join Date
    Jun 2008
    Location
    Europe
    Posts
    1,086

    [resolved]

    Nothing posted thus far has worked, but after two cups of coffee and a bit of Saturday morning sunshine... as well as bouncing ideas off of my good Estonian friend... I got this to work. (Actually found it on a thread elsewhere.)

    This is the entire contents of the .htaccess file:
    Code:
    <IfModule mod_rewrite.c> 
        RewriteEngine On 
        RewriteCond &#37;{REQUEST_FILENAME} !-d 
        RewriteCond %{REQUEST_FILENAME} !-f 
        RewriteRule ^(.*)$ /index.php?$1 [QSA,L] 
    </IfModule>
    Found the answer here:
    http://bakery.cakephp.org/articles/v...shared-hosting


    Now it works like a charm.
    Thanks Opifex for the efforts and help - you usually always nail it sooner or later, I just beat you to it this one time...

    (Pure luck on my part!)


    ----------------------------------------------------------------------------------
    Since I have found 100 other threads on this subject with no good answer, I'll try to put more information in here for posterity...

    I found an alternate way of doing it via PHP here:
    http://www.hotscripts.com/category/p...ion/url-based/
    Was about to try this one when I got lucky with the .htaccess file I posted above:
    http://www.spiral-designs.com/produc...ipts/spiralurl
    (This works like tinyURL's service http://www.tinyurl.com )
    Last edited by donatello; 11-28-2009 at 07:12 AM. Reason: Spelling, added link

  7. #7
    Join Date
    Jun 2008
    Location
    Europe
    Posts
    1,086
    One more thing... don't know if it made any difference as I was trying everything, but I also created a php.ini file with the following contents:

    php.ini
    Code:
    register_globals = on

  8. #8
    Join Date
    Sep 2008
    Location
    Mexico
    Posts
    786
    FANTASTIC!!!
    I'd thought about QSA, but don't have any real experience using it (I get to see it in action now!). How did the final RewriteRule come out?

    The register_globals flag shouldn't affect this (I think).
    ...but stupidity is terminal.

  9. #9
    Join Date
    Jun 2008
    Location
    Europe
    Posts
    1,086
    This did it:
    Code:
    <IfModule mod_rewrite.c> 
        RewriteEngine On 
        RewriteCond %{REQUEST_FILENAME} !-d 
        RewriteCond %{REQUEST_FILENAME} !-f 
        RewriteRule ^(.*)$ /index.php?$1 [QSA,L] 
    </IfModule>
    That is the entire contents of the .htaccess file and it works.
    Don't ask me to explain any of it!

    BTW- What does QSA mean?

  10. #10
    Join Date
    Sep 2008
    Location
    Mexico
    Posts
    786
    QSA = Query String Append
    Whatever you put after the url is translated to a query string server side (?dogs-sweatshirts).
    Thats why the new links that work are tank-tops.net/dogs-sweatshirts no need for the products folder emulation which I think is really a lot nicer for your site. You could add a search box that would just append whatever was entered to the base url.... hmm... "linux" returned a nice selection
    ... played a little and the query append MUST have 4 characters minimum and cannot accept an asterisk(*) or it returns a fail to open string warning... and a or slash(/) will call a folder of course. That can be sorted easily enough.

    I have never played with QSA, but it is nice! I can think of a couple of places that this could come in handy. (One more proven tool in the arsenal!)

    Oh, you can take out the "IfModule" tags if you want... that's only needed to avoid errors IF the module ISN'T installed.
    Last edited by opifex; 11-28-2009 at 08:35 PM.
    ...but stupidity is terminal.

  11. #11
    Join Date
    Jun 2008
    Location
    Europe
    Posts
    1,086

    [resolved]

    Thanks a lot for the clarification! Awesome tool.
    I have a lot to learn, but love this stuff!

    You sound like a professor of computer science..

  12. #12
    Join Date
    Sep 2008
    Location
    Mexico
    Posts
    786
    SHHHHH.....
    That's one of my day jobs.... but looks like I'll have time to do more of my other work next semester due to budget cuts. Maybe I'll translate my syllabus and course materials to English.
    ...but stupidity is terminal.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles