tags:

views:

328

answers:

4

Hello there,

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.

A: 

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>
Nicky De Maeyer
thanks for your answer Nicky, but i dont see html suffix in the code above. anyways i havent tried that yet.
Sarfraz
Hello All, any more ideas please?????
Sarfraz
A: 

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>
Steve Claridge
thanks for your answer Steve, but you probably did not understand my question correclty.I don't want to remove html access, i want to add it to each URI but i wonder how to do it?I am using a framework which automatically generates URIs based on route. So i can not put in links with html suffix added to them.I hope i clarified this time aroud.Still looking for a solution !!
Sarfraz
simply, how to add html suffix to each url?
Sarfraz
Are you asking to have all <a> tags appended with a .HTML suffix? htaccess won't do that, you need to create your URLs from your PHP framework and then convert them BACK into a call to index.php in htaccess. Sounds like you need to update your framework.
Steve Claridge
+2  A: 

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.
Tim Lytle
+1  A: 

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.

David W
yo right David, finally i have come up with that solution :) Thanks to all who replied...
Sarfraz