tags:

views:

407

answers:

4

Hi,

I think this is a pretty straight forward question in mod_rewrite:

I got one domain, which needs to redirect to another, but keep any value after last slash (/) in the first URL, over to the second.

domain.com/4433 should transfer to domain.com/folder/?p=4333

Listed for clarity:

From: domain.com/4433

To: domain.com/folder/?p=4333

Any ideas?

Edit: Did some testing, we found the following solution:

RewriteCond %{HTTP_HOST} ^domain.com$

RewriteRule ^([0-9a-z]*)$ /folder/?p=$1 [NC]

sincerely, - bakkelun

A: 
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ domain.com/folder?p=$1 [R=301,L]
Zed
If both domains run with the same vhost, then this will give an endless redirect loop.
Residuum
@Residuum: Not just in that case. The *Substitution* is just a relative path. So `/4433` would be redirected to `/domain.com/folder?p=4433` that would be redirected to `/domain.com/domain.com/folder?p=domain.com/folder` that would be redirected to `/domain.com/domain.com/domain.com/folder?p=domain.com/domain.com/folder` etc.
Gumbo
Thanks guys for commenting on my careless answer. I tried to put in a (perhaps another careless) fix. I guess the real solution would then depend on how the complete set of valid urls look for the domain.
Zed
A: 

Just in case: domain.com = domain1.com and domain2.com? domain1.com should be redirected to domain2.com? Both run on the same server (optional)?

[EDIT:]

If you really only want to do the thing as stated in the comment, then do the following:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^4433$ http://domain2.com/folder/?p=4433 [R=301,L]

Else, as Benedikt Eger said, or with R=301 if you want real redirection. Or, if you want it to redirect only on numbers, then do the following:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^([0-9])+$ http://domain2.com/folder/?p=$1 [R=301,L]

RewriteCond checks, if defined vhost is domain1.com, but not domain2.com, then the rewrite rule is applied, and redirects via HTTP status 301 [R=301] only number strings (0-9)+ consisting of at least one number to the specified URL. [L] makes this the last rule applied.

Residuum
Perhaps I forgot to mention it, but ONLY accesses to domain/4433 should redirect to domain.com/folder/?p=4333"domain.com" alone should not redirect.
bakkelun
You mixed something up. `[(0-9)+]` should rather be `([0-9])+`.
Gumbo
Thanks, Gumbo. I have fixed that typo / thinko.
Residuum
A: 

In case you don't really want to redirect but to have pretty URLs, you can use

RewriteEngine On
RewriteRule ^/(.+)$ /folder?p=$1 [L]

This takes everything after the first slash and inserts it at the $1 - but only if there's something after the slash. It doesn't issue a redirect so the users won't notice.

Benedikt Eger
A: 

Without any further information, try this:

RewriteEngine on
RewriteRule ^/([^/]+)$ /folder/?p=$1

If you want to use the rule in a .htaccess file, remove the leading slashes.

Gumbo