Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite and .htaccess

Newbie at php, mod_rewrite and .htaccess

         

OutdoorMan

5:35 am on Jan 21, 2008 (gmt 0)

10+ Year Member



Hi,

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.

Marcia

6:25 am on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check the forum library for some basic tutorials, particularly this one on regular expressions:

[webmasterworld.com...]

Marcia

10:03 am on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Via the variable 'blue-widget' I wish to get data from a mySQL database and load the data into '/cat.php'.

That would be a programming function (PHP, ASP, etc.), mod_rewrite doesn't open, fetch, move and load data from and to files and web pages, but deals rather with the "handling" and presentation of URLs that are delivered to user agents by the server.

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.

OutdoorMan

12:56 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Thanks, Marcia. But as mentioned, I allready have read and tried several tutorials etc. and still it doesn't work for me.

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?

jdMorgan

1:51 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



URLs 'seen' by RewriteRule in .htaccess are stripped of the path to the directory in which that .htaccess code resides. In simple terms, that means that there will be no leading slash on the URL-path you are trying to match in your RewriteRule.

So the first thing to try is to remove the leading slash from your RewriteRule pattern.

Jim

OutdoorMan

2:23 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



jdMorgan > Thanks

Like this?

php_value register_globals off

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-F
RewriteRule ^cat/([0-9]+)$ /cat.php?id=$1

I get a server error:
"500 Internal Server Error"

jdMorgan

3:20 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look at your server error log...

Jim

OutdoorMan

3:47 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



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.

jdMorgan

4:05 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can make the compare case-insensitive to shorten the pattern and save processing time, and always use the [L] flag unless you have a specific reason not to do so:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cat/([a-z0-9]+)$ /cat.php?id=$1 [NC,L]

Jim

jdMorgan

4:08 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ask your host for the location of your error logs. These should be available on any decent hosting service -- including shared virtual hosting. It's best not to get into server configuration changes (like mod_rewrite) unless you can access your error logs -- A single typo can cause many subtle problems, and they can be very difficult to identify without full log access.

Jim

OutdoorMan

4:12 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Thanks again, Jim.

But now I get a 404.

Could this be caused by the type of parameters I'm using?:

'/cat/Widget' or '/cat/Widget-blue'

(first letter is uppercase and sometimes there's a "-" in the parameter)

jdMorgan

4:29 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

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

OutdoorMan

8:21 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Thanks, 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

jdMorgan

8:59 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are not going to find examples for every possible combination of regex 'tokens' -- Imagine the number of possible combinations! Rather, you must 'construct' your patterns from the information provided.

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]

First character must be an uppercase letter, following one or more characters must be lowercase letters or hyphens, last character must be a lowercase letter:

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

OutdoorMan

10:34 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Thanks, again.

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]

jdMorgan

11:01 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, now look up all the "quantifiers" to complete the picture... +,?, *, and variations on "{1,3}" :)

Jim

OutdoorMan

11:40 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Great, thanks a lot for your help, Jim :)