0

Possible Duplicate:
Access GET variables with PHP + .htaccess

I am trying write a combination of .htaccess and PHP script to do the following:

If the URL = http://mysite.com/XXXXXXX Redirect to http://mysite.com/subdir/index.php?var=XXXXXXX

Where XXXXXXX is any alphanumeric string 7 characters long without a .php extension.

This is what I have so far,

RewriteEngine On
RewriteRule ^(.*)$ index.php?code=$1
RewriteRule ^subdir/(.*)$ index.php?code=$1 [L,QSA]

Any help would be greatly appreciated.

UPDATE: Thanks for the replies, the link helped but I think where I am having trouble is the condition.

RewriteEngine On
RewriteCondition [HAS 7 ALPHANUM CHARS]  <--HERE
RewriteRule ^(.*)$  /subdir/index.php?code=$1 [QSA,L]
Community
  • 1
  • 1
Nick Pulido
  • 41
  • 1
  • 6
  • @hakra: I'm not so sure this is a duplicate of that question. It looks like the OP here is trying to redirect urls with one segment, which are alphanumeric string 7 characters. Nick, can you elaborate a bit more? Is this the only thing you've tried? I don't see anything here in your code that suggests you're looking for 7 alphanums. – Wesley Murch Aug 27 '12 at 22:22
  • @WesleyMurch: Then suggest some other question as a duplicate please, so I might see what you mean. And you can not ask every question once that can be expressed with the regular expressions mod_rewrite offers. – hakre Aug 27 '12 at 22:24
  • @hakra: Sorry I'm not aware of one off hand, htaccess questions are often asking for something very specific, I always find it difficult to find duplicates, but usually piece together a solution from several posts. – Wesley Murch Aug 27 '12 at 22:26
  • http://httpd.apache.org/docs/current/rewrite/intro.html – hakre Aug 27 '12 at 22:26

1 Answers1

1

Try simply:

RewriteEngine On
RewriteRule (^|/)([a-z0-9]{7})/?$ index.php?code=$2 [L,QSA,NC]

The regular expression (^|/)([a-z0-9]{7})/?$ makes sure to match the very last part of a URI, after the last / and having exactly 7 numbers/letters.

So these will match:

/123abcD
/123abcD/
/something/098eif2
/foo/bar/path/1q2w3rT
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • But also `http://example.com/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX` - to not leave this unnoticed. – hakre Aug 27 '12 at 22:33
  • Perfect! This will work for what I need to do. I'll let PHP handle the rest! Thanks! – Nick Pulido Aug 27 '12 at 22:36
  • @hakra you're right, fixed it with `(^|/)` – Jon Lin Aug 27 '12 at 22:37
  • This worked great for my needs, I'll let PHP handle the rest. I appreciate the LONG case check @hakara. The StackOverflow community is not to be underestimated! – Nick Pulido Aug 27 '12 at 22:37