Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2007
    Posts
    106
    Thanks
    19
    Thanked 12 Times in 12 Posts

    Help with .htaccess RewriteRule regex

    There's a page on my website to view a user's profile. The username is passed in through the URL and since that's all that's needed in the URL I simply pass it as the query string, such as http://example.com/profile/?manwithahat

    I thought it would be nice to get rid of the question mark in the URL by using the Apache RewriteEngine, so the URL could be like this http://example.com/profile/manwithahat

    But I am having trougble with the regular expression that RewriteRule needs. I thought I could use -

    RewriteEngine on
    RewriteRule ^(.*)$ ?$1 [QSA,L]

    but this causes an internal server error.

    Doing some testing I found that this will pass through a number the way I need it to -

    RewriteEngine on
    RewriteRule ^([0-9]+)$ ?$1 [QSA,L]

    so that if the URL is http://example.com/profile/123456 then in PHP the $_SERVER['QUERY_STRING'] value becomes 123456. From this test I know that the overall structure of the RewriteRule is correct, it's just the regular expression that needs fixing.

    What should be used instead of ^(.*)$ ?
    Last edited by >ssp-cdr<; 11-05-2012 at 12:23 AM.
    The CodingForums Funny Images thread ** Now with 20+ pages!! ** - http://www.codingforums.com/showthread.php?t=245405

  • #2
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    could you use

    RewriteEngine on
    RewriteRule ^profile/(.*).html$ /index.php?username=$1 [QSA,L]
    <DmncAtrny> I will write on a huge cement block "BY ACCEPTING THIS BRICK THROUGH YOUR WINDOW, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE INSTALLATION OF THIS BRICK INTO YOUR BUILDING."
    <DmncAtrny> And then hurl it through the window of a Sony officer
    <DmncAtrny> and run like hell

    Portfolio, Tutorials - http://www.nomanic.biz/

  • #3
    Regular Coder
    Join Date
    May 2007
    Posts
    106
    Thanks
    19
    Thanked 12 Times in 12 Posts
    That doesn't fit my specific case.
    The CodingForums Funny Images thread ** Now with 20+ pages!! ** - http://www.codingforums.com/showthread.php?t=245405


  •  

    Posting Permissions

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