Mod Rewrite query string to clean url

Posted on 2012-08-14
Last Modified: 2012-09-18
Hey guys,

Quick question, I have a page which I am trying to mod rewrite to produce a clean url, however I cannot seem to get it to work in htaccess. The old url is:


And I would like it to go to:


The current rule is as follows:

RewriteEngine On
# remove php extension if not a physical file or directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

#rewrite URL
RewriteRule ^test/view/$ test/view?q=$1 [L]

Open in new window

This is on a private server so I don't need to worry about duplicate content etc.

Any pointers or help would be most awesome :)


Question by:MichaelT_
    LVL 49

    Expert Comment

    by:Julian Hansen
    How about this?

    RewriteRule ^test/view?q=(.*) test/view/$ [L]

    Open in new window

    LVL 5

    Author Comment

    Hi JulianH,

    Thanks for taking for taking the time to look at the question and for your response :)

    Unfortunately I still seem to get a 500 Internal Server Error when I add it to .htaccess and then try and access:

    <a href=view/<id> >View Record</a>  For example <a href=view/555 >View Record</a>

    I removed "test" from the path (it is the current directory where the two php files sit) and now my htaccess looks like:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*) $1.php [L]
    RewriteRule ^view?q=(.*) view/$ [L]

    Open in new window

    I have attached a couple of test files I was using to test out the rule as well as the current .htaccess file, it may make it easier to debug what is going wrong.  

    Again thanks for your help :)
    LVL 26

    Accepted Solution


    For a start your new rule needs to appear before your catch all:

    RewriteRule (.*) $1.php [L]

    Else it will never be reached. Also your lacking the expression to instantiate $1, so your q= CGI variable would always be blank, if the rule were reachable. Anyway a very quick hack and the following will tick a few more boxes.

    RewriteEngine On
    #rewrite URL
    RewriteRule ^test/view/(.*)$   test/view?q=$1 [L]
    # remove php extension if not a physical file or directory
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*)  $1.php [L]

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Article by: kevp75
    Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now