Subdirectories URL Internally Redirect to Query String

The URL in the browser would be:

The actual page rendered by the server would be:

RewriteEngine on
RewriteRule ^index/([^/]+)/([^/]+).php /page.php?search=$1&sort=$2 [NC]


  1. User Avatar
    Permalink to comment#

    I think you messed up a little, the URL in browser example wouldn’t match the Rewrite Rule. The URL in browser example would have to be “”

  2. User Avatar
    Permalink to comment#

    I like the idea of a clearer URL, but still I think keeping ‘index’ or ‘index.php’ as part of the URL has very little added value. I’d prefer something like instead.

  3. User Avatar
    Swastik Solutions
    Permalink to comment#

    Nice one, thanks chris.
    Its what I was finding for as it is very useful in php based website and seo friendly too.
    The Reference URL is giving the full details. Thanks for providing it too.

  4. User Avatar
    Permalink to comment#

    Hey, Chris!

    I’m struggelin and messing around these .htaccess, but I couldn’t write them as they work. I have mod_rewrite enabled, and I double checked that RewriteEngine is on at .htaccess file.

    In example, I have one project, where, i have sections and categories, and link looks like this: http://localhost/eshop/index.php?section=fruits&category=apple
    How I could rewrite this link to get it look like this: http://localhost/eshop/fruits/apple

    And also I have similar situation inside WordPress.
    I’ ve made theme, where I made a new engine for some kind of inside social network, but anyway.
    For example, link looks like this: http://localhost/liepa6vsk/klases/?classroom_id=5&page=gallery
    I would like to make it appear as http://localhost/liepa6vsk/klases/classroom_name/gallery

    What I should write in .htaccess to do that, and still not crush previously written Rewrite rules for wordpress?

    One last question: Does link will appear nice, when in link will be only http://localhost/liepa6vsk/klases/?classroom_id=5 (as http://localhost/liepa6vsk/klases/classroom_name/)

    If you gonna solve my questions, you will be like God for me :)

  5. User Avatar
    Permalink to comment#

    @Robertr: the following code in your .htaccess file will fix the first problem:

    RewriteEngine on #unless the rewrite engine is already declared
    RewriteBase /eshop/ #if you dont have a rewrite base, it can cause issues with sub-directories.
    RewriteRule ([^/]+)/([^/]+) index.php?section=$1&category=$2 [NC]

  6. User Avatar

    What if we want ?site={siteslug} to redirect to *

  7. User Avatar
    Permalink to comment#

    htaccess file works only on apache server, but i am currently using ASP that doesn’t run this file through IIS.. so any other ways to rewrite my URL??

  8. User Avatar
    Sunny Singh
    Permalink to comment#

    You can also just use $_SERVER["PATH_INFO"] to get everything after /index.php/.

  9. User Avatar
    Permalink to comment#

    thanks everybody

  10. User Avatar
    Animesh Kumar
    Permalink to comment#

    can any one suggest, what should i do, if i want the usernames to be a part of url..
    such as http://localhost/animeshkumar/
    instead of http://localhost/profile.php?userID=1

    where userID : 1 has the username : animeshkumar in the database..

  11. User Avatar
    Amit Yadav
    Permalink to comment#

    How to remove the php extension on go daddy server?
    OptionMultiviews On does not work for me..:(

    • User Avatar
      Arul Dave
      Permalink to comment#

      “How to remove the php extension on go daddy server?”
      Hi Amit Yadav,
      It doesn’t matter who is the hosting provider.
      It only matters whether your web host has installed Apache server or not.
      Apache sever supports dynamic URL rewriting technique (ModURL Rewrite)

  12. User Avatar
    Permalink to comment#

    RewriteEngine on
    RewriteRule ^index-([^/]+)-([^/]+).php /search.php?search=$teachers&sort=$ohio [NC]

    Using this above code will rewrite the query string URL$teachers&sort=$ohio into

  13. User Avatar
    Jeet Gill
    Permalink to comment#

    any one help me in .htaccess? open the portfolio page in woirdpress.

    but show 404 (page not found error), but i want to open same page when this url is open.


  14. User Avatar
    Permalink to comment#

    RewriteRule ^([a-zA-Z0-9-/]+)/$ sample.php?pageid=$1

    jquery file is not working with httaccess redirect any one please help me…

  15. User Avatar
    Permalink to comment#

    hi, how to create urls which display at google search results as below › Code Snippets › HTAccess
    instead of…/htaccess-seo-friendly-urls-for-all-subf

  16. User Avatar
    Permalink to comment#

    URL should look like http://localhost/test/url-2.html but content should come from http://localhost/test/url?p=2

    please help.

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.