0

I'm trying to rewrite all the URL on my repertory with HTACCESS. I want them to have the same model/architecture. I found some code on different websites, but there are not working when I try them.

Here is what I want to do :

  • I have this URL : localhost/projects.php?id=1
  • I want it to look like this : localhost/projects/1

  • Here is another exemple :

  • I have this URL : localhost/profile.php?id=1
  • I want it to look like this : localhost/profile/1
  • In other words I want to transform this :

    • localhost/page.php?param=value&parm2=value2&...

    to

    • localhost/page/value/value2/...

    Is there any way I can do this with all my pages ? I don't know what I should write on my HTACCESS file.

    1
    • do you have a live domain? Commented Jan 30, 2016 at 23:13

    1 Answer 1

    1

    First of parsing all route with .htaccess will be a bloody pain, you should just handle requested file and its parameters then you should parse parameters inside your php code (this is called as routing) and you will create your routing for your application.

    Please check the .htaccess rule below;

    #These are our conditions to run our rewrite rule RewriteCond %{REQUEST_FILENAME} !-f #Request is not a file RewriteCond %{REQUEST_FILENAME} !-d #Request is not a directory RewriteCond %{REQUEST_FILENAME} !-l #Request is not a link #This is our rewrite rule RewriteRule ^([a-zA-Z0-9]*)?/(.*)$ $1.php/$2 [QSA,L] #QSA means QueryStringAppend modify according to your need, L means this is my last rule which same with stop processing rewrite rules 

    And you should have a php code something like below which process the request for your routing

    <?PHP $requestedPath = trim($_SERVER["PHP_SELF"],"/"); $request = explode("/",$requestedPath,2); $requestFileName = $request[0]; //This is your requested file as you know already from .htaccess $requestParams = $request[1]; //You should parse this params according to your routing need ?> 

    Hope these informations helps you about both routing and generic .htaccess rule.

    PS: Your request will be like www.example.com/myaction/first-parameter/second-parameter/third-parameter and for this example $requestParams will be first-parameter/second-parameter/third-parameter

    Sign up to request clarification or add additional context in comments.

    3 Comments

    Comments starting with // are not allowed in apache.
    No, QSA does not stand for QueryStringAllowed, it stands for Query String Append (see also: RewriteFlags/QSA)
    @MikeRockett oh some how I remember it as query string allowed thank you for right information

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.