views:

58

answers:

3

I want to redirect this:

foo.com/xxx

... to this:

foo.com/somepage.php?id=xxx

This is how I do it:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)$ http://foo.com/somepage.php?id=$1 [L]

the problem now, is that foo.com doesn't work any more. I can't see foo.com neither foo.com/index.php

help me! :)

+1  A: 
RewriteRule ^/([^/]+)$ /somepage.php?id=$1 [L]

Be careful tho, foo.com/1 is a BAD move on SEO.

Ben
The RewriteRule shouldn't contain the domain name. But the question was so vague for all I know foo.com is part of the URI, like `http://bar.com/foo.com/xxx`...
Josh
He specifies [http://]foo.com/somepage.php?id=$1, but for portability, you're right. Edited.
Ben
But in an .htaccess file I don't believe the RewriteRule *can* match the domain name... Maybe I am wrong. Anyway what you have looks correct, so +1 :-)
Josh
Yeah, you can. It is often used to do a 301 from www domains to non www domains and vice versa. You got to be careful tho, its infinite loop prone.
Ben
A: 

Try:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(xxx=)$ /somepage.php?id=$1 [L]

Of course you didn't clearly specify what xxx= is...

Josh
+1  A: 

1) You don't need the RewriteCond line. It's intended to determine WHEN your rule applies, not WHICH PART of the request.

[EDIT] UNLESS, your RewriteCond is there to make this rule apply whenever the query string is empty. If that's it, then there's nothing wrong with it.

2) I think you need to include the first / in your rule, like this:

RewriteRule ^/([^/]+)$ /somepage.php?id=$1 [L]

[EDIT] Both your version and mine will match your index.php file, which might explain why your site is broken right now. If you don't want to match php files, you could add another condition like

RewriteCond ${REQUEST_URI} !^.*\.php$

3) For a rule like this, it might be helpful to add /? at the end (outside the parens, before the $), in case they look for foot.com/xxx/, which makes sense if you want it to look like a directory.

grossvogel
+1 for your first point. As for if your RewriteRule is correct, who knows, the question was way too vague.
Josh