Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Clean URLs with PHP

.htaccess php clean url seo friendly

  • Please log in to reply
38 replies to this topic

#13 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 30 April 2013 - 05:08 PM

Excepting that in those cases what you have for code wouldn't work, so why would you even TRY to deploy THIS code there?

 
well, I see no problem in checking the availability of the data and exit if there is none to deal with... I think it's a good habit to learn.
 

Perhaps... but again since it would be non-functional and would need a rewrite for other systems of doing it, what's the point of adding that check to code that you wouldn't RUN on non-apache systems without a rewrite?
 
IMHO too complex for a tutorial version -- you're gonna confuse/lose people on that.

 
Maybe I am, I still don't think it's complex to put the code in a function. It isn't beginners level, but then the subject is a bit over beginners level to start with.
 

I think that loose/generic is where you lose people -- it's a lesson I learned the hard way back in the '90's writing tech manuals for Marstek. Assume the person learning knows nothing, fail to do that they'll learn nothing. "generic" code can be too case non-specific for the beginner to turn into a working example on their own -- which is where I think "lokilust" got lost. I'm pretty well... seasoned at this, and I'd have a hard time turning your tutorial into a working example!

 
really? strange. I pasted those two code blocks into the files and it ran right away. Where was the trouble, as you are looking with other eyes at my code?
 

Which is where we differ -- User friendly is good to a point, but not when it ends up leaving the barn door open. Is it REALLY so hard to whitelist allowed files? No, it isn't... anyone who can't manage that, probably has no business using these methods in the first place; and if that means "You have to be 'this' smart" to use it? OH WELL.

 
Well, my code don't execute anything either. It just parses the URL. Nothing else. So the security issues are to deal with in another part of the code. But, yes, it let all files and directories be as they are and be executable if they are. I should problably have informed of that! Good point!
 

Not 100% sure what that statement has to do with the block it was quoting -- only thing I can figure is my safename routine -- and you're passing values in the URL, a subset of US-ASCII (characters 32..127) is all that's valid there in the first place, you want other language characters you'd have to have them as entities ANYWAYS! Wait? Are you referring to getData, something mine doesn't need to parse? Yeah, your next part:
 
Don't have to with mine as $_GET is parsed normally regardless of how my redirect handles it. You send mine getdata, $_GET exists and is filled in properly. That's why I strip it clear off before parsing $_SERVER['REQUEST_URI'] as we don't need it!

You do /test/west?best=vest on mine, and $_GET['best'] will return 'vest', because of how my .htaccess method is crafted.

It might be cute to store the HASH if present though -- since you ARE correct there, more datapoints is a good thing... It's just yours seemed to be reinventing the wheel on a lot of things.
-- split -- too many quotes error? REALLY?

Having to switch to italics because of some stupid "too many quotes on merge" error in this forum software.


Well, true, $_GET will work as usual. Still, I see a point in showing the parsing of all parts of the URL. But you're right, that part isn't needed.

That's pretty bad. *sigh* I'll notify Roger and see if it's hard coded or a setting.
 

Well, that's not REALLY how I'd do it -- I'd have a singleton with getters, but that's a bit complicated for demo code for beginners.

 
True, true, but this code could be a method to run in that class.
 

Thing is this is a append in any case, might as well have a working demo they can integrate or work from -- Though IMHO (YMMV) this technique is too specific in function to be used as anything BUT the core of a system. Trying to mix and match or integrate it to existing code is just asking for it to be bloated, broken and insecure... Again though, YMMV. My experience says otherwise.

 
Well, yes, the core parts of the system is probably where you want this kind of functionality, That's why I wanted it to be as separated as possible while still working.
 

Which given the illegible colors, narrow little stripes, broken wordwraps of posting code on a forum, much less breaking up the code so you can't follow the logic or indents, it's HARDER to work from in my experience. That's why in addition to the .rar download there's the viewable source directory full of phps files:
http://www.cutcodedo...viewableSource/

with HEAVILY documenteds source:
http://www.cutcodedo...urce/index.phps

 
Hm. to striped? That's why I had the complete code blocks to start with, and then processing them line by line to try to explain what they do. I think that is a good way of showing what's happening, but maybe it's not so good? About colors, is it good or bad with syntax highlighting? I like it. especially in a tutorial so you can easily see the different parts of the code. But, maybe it gets the code harder to read?
 

I could see how lokilush could have had issues -- A lot of what you are doing over-complicates it -- I know how to do this, and I had trouble making sense of yours. It's... very different from what I'm used to seeing, especially all that futzing around with UTF-8 encode/decode and getData parsing; something my technique doesn't even need to bother with since $_GET is preserved/working... I didn't even figure out that's what you were doing until you mentioned it separately!

It is a good point about it polluting the global namespace though -- Was thinking it might make sense to put what I have as the $ACTION array into $_GET (you can add values to $_GET), or as you did wrap it in a function, but that's the sort of thing I'd expect people to be able to figure out on their own... again real world I'd have it in a singleton with getters, the only setter being the constructor -- so that if it's called more than once it only runs the parsing code once; but I wouldn't put that in THIS tutorial except as a 'lesson 2'... which might not be a bad idea.

Yes, the utf8 part is over-complicating things, you are right. but still, as I just said, I wanted to parse the whole URL. But still, $_GET does it for you (and even better), that's very true.

On the other hand, with just this part, it can be ran over and over again without doing any harm. just taking extra time. But of course, putting it into a initialization class will give the next level to it, as you say, lesson 2. Or 5.
 

I think that's a lot of it, we have different expectations of what people are able to figure out on their own. Comprehension varies from person to person, which is why it helps to have different views and approaches. Yours would work for some, mine others. Really there's room for both.

 
Different views on matters are always a good thing, but I believe they are better off discussed part by part like this, so each part can be discussed one by one. On the other hand, these long posts are somewhat hard to follow as well.


  • 1

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#14 lokilust

lokilust

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 30 April 2013 - 07:37 PM

I think both are a good examples for a newbie like my self to start from ;)


  • 0

#15 hvholst

hvholst

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 25 September 2013 - 04:47 PM

JasonKnight,

 

I like your friendly url code. There is only one thing that i don't like. I used microtime to calculate the time taken by the code. Between the include of the page and the output of the footer a lot of time passes in comparison to the time the index file takes. From start till include is around 0.000500. Total time is around 1380000000.000000. I just can't figure out what the problem is. 


  • 0

#16 hvholst

hvholst

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 26 September 2013 - 02:48 AM

To make sure the problem is in the template.common.php file i created separate files for the header and footer, and included these files in the pages instead of calling the template_header() and template_footer() function. The result is a total time of ~0.000800, but with around 30kb more memory usage. So your template.common.php seems to result in a little less memory usage (691kb vs 723kb) but a lot more parsetime. 


  • 0

#17 UsmanShehu

UsmanShehu

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 26 September 2013 - 06:31 AM

an excellent exposure there...


  • 0

#18 TheodoreTheodorou

TheodoreTheodorou

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 21 December 2013 - 03:04 AM

hello and thanks for this article.

i have a problem:

 

any requests for a url with more than one segment cause all linked assets (css, js, images) and hyperlinks on the page to break.

 

pls help

 

many thanks


  • 0

#19 RichardJarrett

RichardJarrett

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 27 December 2013 - 03:29 PM

I have the issue where that if I use the address bar without a trailing slash:

 

 

http://websiteadress.com/something

 

It works and the page comes up, yet when I add a trailing slash to the end of it:

 

 

 

http://websiteadress.com/something/

 

The page breaks and none of the links, in fact the whole of center page (e.g index.php) get's destroyed and it just shows the content page. Not sure if that makes any sense, but the question is how do I make it work with trailing slash?

 

Thanks for help.


  • 0

#20 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 02 January 2014 - 11:45 PM

I would do something like this in the beginning:

$uri = rtrim($_SERVER['REQUEST_URI'], '/');

And then use $uri instead to just remove the trailing slash, as it's not needed.


hello and thanks for this article.

i have a problem:

 

any requests for a url with more than one segment cause all linked assets (css, js, images) and hyperlinks on the page to break.

 

pls help

 

many thanks

 

Hm, do you have the Rewrite conditions correct? The second line there should avoid any filenames to be rewritten... Do you have a basepath or something else set up?


Edited by Orjan, 02 January 2014 - 11:36 PM.

  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#21 FernandoSilva

FernandoSilva

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 04 March 2014 - 05:37 PM

Thanks both Orjan and JasonKnight for the tutorials and discussion. Reading the discussion between two people that know what they're talking about really helps to try and evaluate the pros, cons and differences of opinion.

 

I'm a novice programmer that decided to google "friendly urls" to try and implement that in my project. I had no idea what I was getting into xD

 

I'm still trying to get my head wrapped around all this, but I won't post any questions until I get my hands dirty and I still need to read up a bit more before I figure out which way to go on the subject, might even pop a 3rd version script on the subject, too soon to tell.

 

-- Back on topic --

 

It is a good point about it polluting the global namespace though -- Was thinking it might make sense to put what I have as the $ACTION array into $_GET (you can add values to $_GET), or as you did wrap it in a function, but that's the sort of thing I'd expect people to be able to figure out on their own... again real world I'd have it in a singleton with getters, the only setter being the constructor -- so that if it's called more than once it only runs the parsing code once; but I wouldn't put that in THIS tutorial except as a 'lesson 2'... which might not be a bad idea.

 

However I am also trying to get into OOP and MVC frameworks (barely scratched the surface though), but I've read up on design patterns and would like to take you up on the idea of using the singleton pattern to implement this behavior.

 

Hope you have the time for it.

Best regards,

Fernando Vieira da Silva


  • 0

#22 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 11 March 2014 - 05:45 AM

Well, one way would be to have a static member storing the outcome of the path parsing and just check if the member already is populated or not in the constructor before running the parser. You could add a few getters to get specific parts from the member and you will probably have what you want.

 

I built this into a router with "static" paths, a database table with path, and what class and action to perform if that path is active, and if no path was found in the table, it falls back to having it like this /class/item/action (or if there are only two parts, /class/action, only one part /class with a preset default action). Just make sure that a class/action just isn't ran out of the blue, as that might compromise the security of the whole system


  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#23 hvholst

hvholst

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 02 April 2014 - 06:29 AM

Since JasonKnight seems to have left us, does anyone else have any idea why his code needs more time than Orjans code? 

 

JasonKnight,

 

I like your friendly url code. There is only one thing that i don't like. I used microtime to calculate the time taken by the code. Between the include of the page and the output of the footer a lot of time passes in comparison to the time the index file takes. From start till include is around 0.000500. Total time is around 1380000000.000000. I just can't figure out what the problem is. 

 

 

To make sure the problem is in the template.common.php file i created separate files for the header and footer, and included these files in the pages instead of calling the template_header() and template_footer() function. The result is a total time of ~0.000800, but with around 30kb more memory usage. So your template.common.php seems to result in a little less memory usage (691kb vs 723kb) but a lot more parsetime. 


Edited by hvholst, 02 April 2014 - 06:30 AM.

  • 0

#24 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 04 April 2014 - 02:34 PM

I looked though the code for both again, and I can't tell why his code would take so much longer time... The only thing I can think of is his several string replaces and the file system access when checking if the file exists, but they shouldn't be THAT expensive...
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.






Also tagged with one or more of these keywords: .htaccess, php, clean url, seo friendly