I am trying to create my URL’s to be SEF and I am a little lost. First off let me say that I know that my Rewrite_mod is on because I did the simple test laid out here, http://datakoncepts.com/seo.
Ok, so I write this in my htaccess file -
RewriteEngine On
RewriteRule /(.*)/(.*)/$ info_page.php?page_id=$1&product=$2
and in that referenced page called “info_page.php” I have this mysql statment to query
$sql = "SELECT * FROM info_page where page_id = $selectedPageId";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
I have been working on this for the better part of 2hrs now and I am new to mod_rewrite, or at least trying to dynamically create URL’s in .htaccess that is.
Just as if that URL was what the user actually typed, $_GET[‘page_id’] and $_GET[‘product’] would be populated from the query string, with “drinks” and “pepsi” as values. You could name them whatever you want, you write the RewriteRule.
Dan’s been giving you good information. If you need to learn a little more, check the tutorial linked in my signature as it’s helped members with their mod_rewrite for years.
and from what I learned over the past 2 days of reading, is that I have the ^(.*)/$ part pulling the original uri and then the /aroma/info_page.php?&page_id=$1 [L] telling the server what to change.
I then have in my page called info_page.php this sql statement to pull from the db to replace the uri with information from the db -
$sql = "SELECT * FROM info_page where page_id = $page_id";
I am not that fluent in sql so I am not sure I got that 100%
If there is any additional advice that someone can give me or point me in a direction as to what I might be doing wrong, it would be greatly appreciated.
ARGH! More of the :kaioken: EVERYTHING :kaioken: atom!
Okay, if you’re sure that ([a-zA-Z]+) won’t do it for you, yes, your code will redirect from {EVERYTHING/NOTHING}/ to the server’s root/aroma/info_page.php script. Just know that, because you’re already in the website’s root directory, you do NOT need the leading / in the redirection AND Apache MAY decide you want the server’s root, not the website’s root, DO NOT USE THE LEADING / IN THE REDIRECTION!
$sql = "SELECT * FROM info_page where page_id = '$page_id'";
Because $page_id is a string (unless you used the intvalval() function on it), you MUST enclose it in single quotes. “Close enough for government work” does not apply when coding!
That also assumes PHP’s register_globals setting is enabled, otherwise you need to use $_GET[‘page_id’], as $page_id isn’t created for you automatically
Dan was correct about that (super_globals) except that it SHOULD be disabled as that’s a security problem that PHP has addressed by merely disabling it by default. I had made the assumption that you’d already gotten the $page_id variable with code similar to:
if (isset($_GET['page_id'] && !empty($_GET['page_id'])) $page_id = $_GET['page_id'];
In other words, if you don’t test that it exists, PHP will throw an error; if it’s a null value, your query will be looking for the null (which should not exist). ALWAYS look for ways that someone can break your script when dealing with inputs like this!
:award: Kudos to Dan for adding this to your thread.