Grow your CSS skills. Land your dream job.

Last updated on:

Subdirectories URL Internally Redirect to Query String

The URL in the browser would be:

http://css-tricks.com/index.php/teachers/a/

The actual page rendered by the server would be:

http://css-tricks.com/index.php?search=teachers&sort=a

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

Reference URL

Comments

  1. Dyllon
    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 “http://css-tricks.com/index/teachers/a/”

  2. Rob
    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 http://css-tricks.com/teachers/a/ instead.

    • Tom
      Permalink to comment#

      Its a clearer URL and improves SEO more importantly.

    • Permalink to comment#

      Slight problem. If you want to link to something deeper than the root of your website, it would be passed as an argument in index.php.

      Instead, use a keyword like “search” to give yourself more latitude. The resulting http://css-tricks.com/search/teachers/a/ is SEO-friendly and you only limit yourself to avoiding a folder named “search”.

  3. 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. 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. Dyllon
    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. Adam

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

  7. sgaawc
    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. You can also just use $_SERVER["PATH_INFO"] to get everything after /index.php/.

  9. Permalink to comment#

    thanks everybody

  10. 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. Amit Yadav
    Permalink to comment#

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

    • “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. RewriteEngine on
    RewriteRule ^index-([^/]+)-([^/]+).php /search.php?search=$teachers&sort=$ohio [NC]

    Using this above code will rewrite the query string URL http://yoursite.com/search.php?search=$teachers&sort=$ohio into http://yoursite.com/index-teachers-ohio.php

  13. Hi,
    any one help me in .htaccess?

    http://gilltech.in/portfolio/ open the portfolio page in woirdpress.

    but http://gilltech.in/portfolio/seowebin-free-seo-tool show 404 (page not found error), but i want to open same page http://gilltech.in/portfolio/ when this url is open.

    thanks

  14. ashok
    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. Prakash
    Permalink to comment#

    I’m new here and I got this htaccess code to redirect:

    301 Redirect Entire Directory

    RedirectMatch 301 teamfront.php?(.*) /$1

    It is redirecting fine from:

    http://tresbizz.com/teamfront.php?Adobe-Acrobat-XI-Standard

    to

    http://tresbizz.com/?Adobe-Acrobat-XI-Standard

    but not linking to respective page, anybody please help me.
    this is also fine If it redirects to:

    http://tresbizz.com/Adobe-Acrobat-XI-Standard

    Thanks in advance

Leave a Comment

Posting Code

  • Use Markdown, and it will escape the code for you, like `<div class="cool">`.
  • Use triple-backticks for blocks of code.
    ``` 
    <div>
      <h1>multi-line block of code</h1>
      <span>be cool yo.</span>
    </div>
    ```
  • Otherwise, escape your code, like <code>&lt;div class="cool"&gt;</code>. Markdown is just easier though.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".