FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP and .htaccess





polly-gone
So I have a question about permalinks using .htaccess and mod_rewrite.

Certain sites, like Suite101.com, have all of their article URLs with the article titles in them. What method would they use to do this? Would they stick the title (I.e. this-is-an-article-title) in a query string and just have a rule that converted the query string into the URL, or would they have the article numbered in the query string and have a script add that specific article number as a rule to that URL?

Thanks,

-Nick Smile Smile Smile
Fire Boar
They'd have something that writes every request to a page that doesn't exist to index.php?q=(the url requested) or something like that. So...

http://www.example.com/news/Peppers_are_green

would route to

http://www.example.com/index.php?q=news/Peppers_are_green

It would then use $request = explode('/', $_GET['q']) in a PHP script. In this case, $request[0] = 'news' and $request[1] = 'Peppers_are_green'. It would then include the relevant PHP files and decide what to do from there.

That's just one possible way it could be done.
polly-gone
Yeah, I know about that technique, but I just care about using the mod_rewrite technique.

-Nick Smile Smile Smile
Fire Boar
polly-gone wrote:
Yeah, I know about that technique, but I just care about using the mod_rewrite technique.

-Nick Smile Smile Smile


That is the mod_rewrite technique. One of them, anyway, and the one that I find is the most elegant.
polly-gone
Um... no, that is not the mod_rewrite technique. The mod_rewrite technique uses RewriteRules placed in the .htaccess file.

Like this:

Code:
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]


The true mod_rewrite technique does not leave the original file in the URL. Your way it is like index.php\2010\02\12\article-name-here\. The original file is still in the URL and you see .php.

With the technique that I am looking at, you could have a script write something like this:

Code:
RewriteRule article-title-goes-here/ index.php?articleID=9492012820 [L]


and when the user goes to http://example.com/article-title-goes-here/, the address bar will say http://example.com/article-title-goes-here/, but the actual page that is displaying could be http://example.com/index.php?articleID=9492012820&yourVariable=true&blahBlah=false.

So basically my question is this:

Is it safe to have a script write a directory's .htaccess file?

-Nick Smile Smile Smile
Fire Boar
polly-gone wrote:
Um... no, that is not the mod_rewrite technique. The mod_rewrite technique uses RewriteRules placed in the .htaccess file.

Like this:

Code:
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]


The true mod_rewrite technique does not leave the original file in the URL. Your way it is like index.php\2010\02\12\article-name-here\. The original file is still in the URL and you see .php.


Did I ever say that? No. What I actually said was...

Fire Boar wrote:
They'd have something that writes every request to a page that doesn't exist to index.php?q=(the url requested) or something like that. So...

http://www.example.com/news/Peppers_are_green

would route to

http://www.example.com/index.php?q=news/Peppers_are_green


And, you'll notice, there's no index.php in the example url given. So how do we achieve this? Easy.

Code:
Options -Indexes
Options +FollowSymLinks

# Uncomment these four lines if you want to handle any 404 errors yourself. No ending quote for
# compatibility with Apache 1.3.
#ErrorDocument 404 /index.php
#<Files favicon.ico>
#  ErrorDocument 404 "The requested file favicon.ico was not found.
#</Files>

# Set the default page.
DirectoryIndex index.php

<IfModule mod_rewrite.c>
  RewriteEngine on
  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>


That's the .htaccess file needed, I thought it was implied that you had to write something like that by my post... apparently I was mistaken, so I apologize for the confusion.


As for your question at the end there... it's extremely unsafe to have a script to write to the .htaccess file, and you have to be extremely careful if you try. Far better is to have a routing table or file, then map each URL to a corresponding page ID. It would be safe to have your PHP script edit the file or do the database queries (database would be the more scalable option).
polly-gone
The confusion came from the fact that the explode() technique that you mentioned before and the technique that you JUST mentioned are two different techniques.

And my question was should I have a PHP script add a RewriteRule to the .htaccess that includes the article page name and that redirects the page, or should I have a variable RewriteRule that pulls the article title from the query string like index.php?title=my-article-title-here.

-Nick Rolling Eyes Rolling Eyes Rolling Eyes
Fire Boar
polly-gone wrote:
The confusion came from the fact that the explode() technique that you mentioned before and the technique that you JUST mentioned are two different techniques.


Code:
function arg($index) {
  static $args = null;
  if ($args === null) {
    $args = explode('/', $_GET['q']);
  }
  return $args[$index];
}


Same technique. The rewrite rule part converts the entire url to a single _GET parameter. The PHP code part that I've just written lets you immediately access any of the arguments, by way of explode. I don't want to argue, just to make clear that this is the technique I've been describing all along.

polly-gone wrote:
And my question was should I have a PHP script add a RewriteRule to the .htaccess that includes the article page name and that redirects the page, or should I have a variable RewriteRule that pulls the article title from the query string like index.php?title=my-article-title-here.

-Nick Rolling Eyes Rolling Eyes Rolling Eyes


Yep. I answered that one too.

Fire Boar wrote:
As for your question at the end there... it's extremely unsafe to have a script to write to the .htaccess file, and you have to be extremely careful if you try. Far better is to have a routing table or file, then map each URL to a corresponding page ID. It would be safe to have your PHP script edit the file or do the database queries (database would be the more scalable option).


Short answer: no, you shouldn't.

You should redirect everything to a script with one rule, like what I posted, then have a routing file or database table to determine where each request should go. Or a mix of coded and routing. For example, if we just want everything that looks like http://www.example.com/article/title-goes-here we check if arg(0) is 'article', if so we look up the article title, arg(1), in the routing table and do database queries as required).


I suggest you experiment with the .htaccess rules and PHP function I've provided - you will find that you can achieve what you're after with it, and it's a LOT cleaner than having a script generate the .htaccess file on the fly. If you're wondering about the PHP code, it's actually more efficient than the simpler:

Code:
function bad_arg($index) {
  $args = explode('/', $_GET['q']);
  return $args[$index];
}


The reason being that the static variable only initializes once, so you're only calling explode on $_GET['q'] once no matter how many times you call arg(). Therefore, the original function arg() is almost as fast as variable access.
Related topics
htaccess Password Protection
CANT UPLOAD .htaccess, .htpasswd, .header, .footer
[tutor] How to protect images without htaccess using PHP
How To : Secure Your PHP Website
.htaccess
Must cpanel be 2082? I can't connect
[PhP] News Posting Tutorial (code, actually ^^')
[SOLVED] PHP is not working
.htaccess problem
Cron jobs - running a php file
PHP + .htaccess
Limiting php file functions with .htaccess
301 redirection in PHP and .htaccess
.htaccess issues with & and ? characters in title
301 Redirect
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.