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]
4
  • @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. Commented Aug 27, 2012 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. Commented Aug 27, 2012 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. Commented Aug 27, 2012 at 22:26
  • httpd.apache.org/docs/current/rewrite/intro.html Commented Aug 27, 2012 at 22:26

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

But also http://example.com/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - to not leave this unnoticed.
Perfect! This will work for what I need to do. I'll let PHP handle the rest! Thanks!
@hakra you're right, fixed it with (^|/)
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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.