Skip to content

Instantly share code, notes, and snippets.

@flacodirt
Forked from RaVbaker/readme.md
Last active December 17, 2015 14:49
Show Gist options
  • Select an option

  • Save flacodirt/5627315 to your computer and use it in GitHub Desktop.

Select an option

Save flacodirt/5627315 to your computer and use it in GitHub Desktop.

Revisions

  1. flacodirt revised this gist May 22, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ The QSA part tells the module to append any query strings to the request.
    The ?q=$1 tells the module how to pass down the parameter. In this case, it's passed down as the q parameter.
    You can extend this even further by using regular expressions. For example:

    RewriteRule ^([^/]*)(.*)$ index.php?first=$1&second=$2
    RewriteRule ^([^/]*)(.*)$ index.php?first=$1&second=$2 [L,QSA]

    This will pass down the first part of the path as the first parameter, and the rest as the second. So the following request

  2. @RaVbaker RaVbaker revised this gist Mar 30, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -53,4 +53,6 @@ If it's not working, make sure that mod_rewrite is installed on Apache. On a uni

    sudo a2enmod rewrite

    to achieve that.
    to achieve that.

    Source [http://jrgns.net/content/redirect_request_to_index](http://jrgns.net/content/redirect_request_to_index)
  3. @RaVbaker RaVbaker created this gist Mar 30, 2012.
    56 changes: 56 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    # Redirect All Requests To Index.php Using .htaccess

    In one of my pet projects, I redirect all requests to index.php, which then decides what to do with it:

    ## Simple Example

    This snippet in your .htaccess will ensure that all requests for files and folders that does not exists will be redirected to index.php:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . index.php [L]

    This enables the rewrite engine:

    RewriteEngine on

    This checks for existing folders (-d) and files (-f):

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f

    And this does the actual redirecting:

    RewriteRule . index.php [L]

    ## Extended Example

    You can extend this to pass the requested path to the index.php file by modifying the RewriteRule to the following:

    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

    The ^(.*)$ part tells the rewrite module that we want to pass down the whole requested path as one parameter.
    The QSA part tells the module to append any query strings to the request.
    The ?q=$1 tells the module how to pass down the parameter. In this case, it's passed down as the q parameter.
    You can extend this even further by using regular expressions. For example:

    RewriteRule ^([^/]*)(.*)$ index.php?first=$1&second=$2

    This will pass down the first part of the path as the first parameter, and the rest as the second. So the following request

    http://yourhost.com/some/path/somewhere

    will result in

    http://yourhost.com/index.php?first=some&second=path/somewhere

    This allows for some creative ways to do clean URLs.

    ## Trouble Shooting

    If it's not working, make sure that mod_rewrite is installed on Apache. On a unix system you can just do

    sudo a2enmod rewrite

    to achieve that.