Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite cant pick up GET variable

         

FiRe

3:15 pm on Oct 20, 2008 (gmt 0)

10+ Year Member



Options +FollowSymlinks
RewriteEngine on
RewriteRule ^post/(.*)/ post.php?slug=$1

I am sure I have done something similar before, but going to /post/test/ cannot pick up "test" in $_GET['slug']

Also is this the best way to write it? I am sure a regular expression would be better, it must only be valid 0-9a-zA-Z and - how would I write this?

[edited by: FiRe at 3:16 pm (utc) on Oct. 20, 2008]

g1smd

3:40 pm on Oct 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



(.*)
is greedy.

You might use

([^/])
as "everything up to next /" or suchlike.

In fact, if the parameter format can be closely defined - length is always a certain fixed number of characters, or is a slightly different but predictable length (you know the shortest and longest length it could ever be), then you could test for that.

If it only consists of letters, or numbers, code for that too, like

[a-z]{6,8}
or like
[0-9]{7}
etc.

It will result in less "duff" URLs being passed to the PHP script. The PHP script should check the validity of all values anyway, but it is quicker for them to be rejected before the PHP handler is even invoked.

Do not make the tests within .htaccess case insensitive unless your script then checks the case of the request and rejects inputs with the wrong case (those should either 404, or 301 to the correct case) otherwise there will be a duplicate content issue where any case of URL will still directly return the content.

FiRe

4:07 pm on Oct 20, 2008 (gmt 0)

10+ Year Member



Thanks g1smd,

I now have this:

RewriteRule ^post/([0-9a-zA-Z\-]+)/ post.php?slug=$1

Which is less "greedy" I suppose. The only issue is it will not pick up $_GET['slug'] I assume this is because post/ and post.php have the same name as this does work:

RewriteRule ^xpost/([0-9a-zA-Z\-]+)/ post.php?slug=$1

g1smd

4:25 pm on Oct 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Uh, "POST" is a reserved word in "HTTP". Is that the problem?

jdMorgan

5:27 pm on Oct 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That should not be a problem with HTTP or Apache, but it *may* be a problem with a firewall or mod_security...

Change the name if possible, otherwise, contact your host.

Jim