Forum Moderators: phranque
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]
(.*) 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.
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