Sarfraz
Sarfraz

Reputation: 382861

URL Rewriting In PHP MVC Framework

I have made my own custom php framework which implements MVC design pattern. In it, all URLs are constructed like this

mysite.com/index.php?route=controller/function/parameters/go/here
mysite.com/index.php?route=products/shirts/99

etc

I have put in place htaccess to remove index.php?route= part from URLs to make them more SEF making them appear like this:

mysite.com/controller/function/parameters/go/here
mysite.com/products/shirts/99

I just want to append a suffix at the end of each URL like so:

mysite.com/controller/function/parameters/go/here.html
mysite.com/products/shirts/99.html

Probably htaccess can be put to use for acheiving that or possibly some other solutions.

Any ideas please?

Thanks in advance.

Upvotes: 1

Views: 4514

Answers (4)

David W
David W

Reputation: 945

Like others, I think the appending needs to happen in your framework.

I imagine that you have a function like: $html->link('products/shirts/99');

That function should add the '.html'.

Your url handler should look like:

//$query products/shirts/99.html

$l = explode ($query, '/'); $last = length($l); $l[$last-1] = substring($l[$last-1] , find($l[$last-1] ,'.html')) //I am doing the args from memory so please check them.

This is how CakePHP's index.php would be modified. I suggest you look at that code as a guide.

Upvotes: 1

Tim Lytle
Tim Lytle

Reputation: 17624

I assume you're taking the 'nice' urls then rewriting them to your index.php with the request rewritten to a get var or 'route'.

There are some problems you'll run into in the future, one will be how to deal with get vars in the request (if I recall dealing with a similar concept years ago).

Personally, I would just setup your .htaccess to send all requests that are not valid files/directories to your bootstrap/index.php file. Then do the parsing in PHP, not in a overly complex mod_rewrite. You're already parsing the 'route' get var, why not just parse the whole request?

At that point the first step of you index.php file would be to:

  • Remove the . extension (or identify the request as html/xml/json/etc)
  • Remove any kind of base URL.
  • Parse the remainder like you do the route get var.

Upvotes: 2

Steve Claridge
Steve Claridge

Reputation: 11090

If I understand you correctly, you say you have used htaccess to remove route= and you want to also remove a .html suffix. I think you are approaching it the wrong way.

I would have all the links in your app pointing to your preferred URLs and then have htaccess PUT IN in the route= and the index.php elements

So a link looks like this:

<a href="mysite.com/controller/function/parameters/go/here.html" />

and you can use an htaccess rule similar to Nicky's ton convert that into:

mysite.com/index.php?route=controller/function/parameters/go/here.html

In fact Nicky's will work fine. You have the option of removing the .html suffix in your index.php code or from the htaccess rule - easier from PHP in my opinion.

If you want to remove .html using htaccess then this will probably work (untested):

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)\.html$ index.php?route=$1 [QSA,L]
</IfModule>

Upvotes: 2

NDM
NDM

Reputation: 6840

I'm not an expert but try this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]
</IfModule>

Upvotes: 0

Related Questions