apache.htaccessurlrewriting

.htaccess : Pretty URL with whatever number+names of parameters



Hello ! I know there already are a lot of topics about URL rewritting and I honestly swear I've spent a lot of time trying to apply them to my problem but I can't see any of them perfectly applying to my situation (if you find otherwise, please give the link). -----

Here's the problem :

I'm learning MVC model and URL rewriting and I have my URL like this :

http://localhost/blahblahblah/mywebsite/index.php?param1=value1&param2=value2&param3=value3 ... etc ...

What I want (for some MVC template goals) is to have this kind of URL :

http://localhost/blahblahblah/mywebsite/value1/value2/value3 ... etc ...

-----

Whatever are the names of the parameters and whatever are the values.
This is the most essential thing I can't find a solution for.

(Also don't mind the localhost blahblahblah, this has to work even on distant websites but I trust it will work fine on online website has this part of URL may have no importance in what I want to do)

Thanks a lot for your time if you can help me seeing clearer in what I need to do.


Solution

  • If the .htaccess file is located in the document root (ie. effectively at http://localhost/.htaccess) then you would need to do something like the following using mod_rewrite:

    RewriteEngine On
    
    RewriteRule ^(blahblahblah/mywebsite)/(\w+)$ $1/index.php?param1=$2 [L]
    RewriteRule ^(blahblahblah/mywebsite)/(\w+)/(\w+)$ $1/index.php?param1=$2&param2=$3 [L]
    RewriteRule ^(blahblahblah/mywebsite)/(\w+)/(\w+)/(\w+)$ $1/index.php?param1=$2&param2=$3&param3=$4 [L]
    # etc.
    

    Where $n is a backreference to the corresponding captured group in the preceding RewriteRule pattern (1st argument).

    \w is a shorthand character class that matches a-z, A-Z, 0-9 and _ (underscore).

    A new directive is required for every number of parameters. You could combine them into a single (complex) directive but you would have lots of empty parameters when only a few parameters were passed (rather than not passing those parameters at all).

    I'm assuming your URLs do not end in a trailing slash.

    If, however, the .htaccess file is located in the /blahblahblah/mywebsite directory then then directives could be simplified a bit:

    RewriteRule ^(\w+)$ index.php?param1=$1 [L]
    RewriteRule ^(\w+)/(\w+)$ index.php?param1=$1&param2=$2 [L]
    RewriteRule ^(\w+)/([\w]+)/([\w]+)$ index.php?param1=$1&param2=$2&param3=$3 [L]
    # etc.
    

    Don't use URL parameters (alternative method)

    An alternative approach is to not convert the path segments into URL parameters in .htaccess and instead just pass everything to index.php and let your PHP script split the URL into parameters. This allows for any number of parameters.

    For example, your .htaccess file then becomes rather more simple:

    RewriteRule ^\w+(/\w+)*$ index.php [L]
    

    (This assumes the .htaccess file is located in /blahblahblah/mywebsite directory, otherwise you need to add the necessary directory prefix as above.)

    The RewriteRule pattern simply validates the request URL is of the form /value1 or /value1/value2 or /value1/value2/value3 etc. And the request is rewritten to index.php (the front-controller) to handle everything.

    In index.php you then examine $_SERVER['REQUEST_URI'] and parse the requested URL.