Forum Moderators: phranque
I'm about to switch from ASP to PHP and now I try to learn how to use mod_rewrite and .htaccess -- but it doesn't seem to work for me at all.
I have tried examples from several mod_rewrite guides, mod_rewrite tutorials etc. on the net (and of course I have read some posts on WebmasterWorld), but still I don't get it.
1) What I wish to do is to call:
ex. http://www.example.com/cat.php?id=blue-widget
from the URL:
ex. http://www.example.com/cat/blue-widget
2) Via the variable 'blue-widget' I wish to get data from a mySQL database and load the data into '/cat.php'.
Can someone please help me? Thanks.
[webmasterworld.com...]
Via the variable 'blue-widget' I wish to get data from a mySQL database and load the data into '/cat.php'.
If it's data you want to process, reading from a MySQL database and writing onto web pages, it sounds like what you'll need is a PHP solution.
I have for example tried something like this, but it doesn't work when I call it from http://www.example.com/cat/[parameter]:
.htaccess:
php_value register_globals off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-F
RewriteRule ^/cat/([0-9]+)$ /cat.php?id=$1
/cat.php:
<?
if(isset($_GET['id']))
{
$id = $_GET['id'];
$alert = "Category is: " . $id;
}
else
{
$alert = "You haven't selected a category";
}
?>
Maybe I'm doing something wrong?
So the first thing to try is to remove the leading slash from your RewriteRule pattern.
Jim
Look at your server error log...
I don't know where to find it or if I have access to view it -- I don't have my own server.
If I should have access to the server error log where do I find it?
---
Anyway, this seems to work:
.htaccess
php_value register_globals off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-F
RewriteRule ^cat/([a-zA-Z0-9]+)$ /cat.php?id=$1
Do you have any suggestions of improvement for the above? Is it 'SEO safe'?
Thanks.
Jim
You could add "\-" to your [character-group] to accept hyphens, or replace the entire group with ".+" to accept any non-blank "cat" characters.
See the concise regular-expressions tutorial cited in our forum charter for more info.
Jim
I've added "\-" to .htaccess and it works fine.
BTW, I called my hosts support hotline and found out that I do have access to the server error log :)
Pick the pattern based on what you are willing to accept as a "cat". Right now, your pattern accepts only letters and numbers. There is no single right or wrong pattern, it depends on what you want to do.See the concise regular-expressions tutorial cited in our forum charter for more info.
I have looked at reg.exp tutorial. But I don't see any info (here or elsewhere) about what I need.
What I need is the knowledge of how to make a pattern that only allows letters, numbers and "-" -- and also makes the cat.php available only if the first letter in the "cat" is uppercase and the rest is lowercase
Maybe I can explain it like this:
/cat/First-letter = OK
/cat/Firstletter = OK
/cat/first-letter = 404
/cat/first-letter = 404
/cat/firstLetter = 404
/cat/first-letteR = 404
First character must be an uppercase letter, following one or more characters must be lowercase letters or hyphens:
RewriteRule ^cat/([A-Z][a-z0-9]+)$ /cat.php?id=$1 [b][L][/b]
RewriteRule ^cat/([A-Z][a-z0-9]+[a-z])$ /cat.php?id=$1 [b][L][/b]
Note that this first new example now requires a minimum of three characters, while the second requires three. That can be changed as well -- using regex tokens. Regular expressions are complicated because they are extremely powerful at matching exact requirements. But you must specify those exact requirements carefully and correctly. So this requires thorough analysis of exactly what you do and don't want to match.
Jim
Yeah, I know... That would be a lot of combinations.
But it sure would be nice to have some sort of reference list made of short examples -- just like the example you've posted here, Jim :)
Now I begin to get the picture (I think):
RewriteRule ^cat/([A-Z][a-z0-9]+[a-z])$ /cat.php?id=$1 [L]
The first [] ("[A-Z]") is the first letter (only uppercase letters).
The second [] ("[a-z0-9]") is the letters and/or numbers between the first and last letter (only lowercase letters and numbers).
And the last [] ("[a-z]") is the final letter (only lowercase letters).
Also if I put the "\-" (to allow hyphens) into the second [] ("[a-z0-9]") I get this:
RewriteRule ^cat/([A-Z][a-z0-9\-]+[a-z])$ /cat.php?id=$1 [L]
Right?
[edited by: OutdoorMan at 10:35 pm (utc) on Jan. 21, 2008]