views:

23

answers:

3

Hi,

I've been fighting with mod-rewrite for a while.

Basically, I have a website that I'm moving to a difference namespace/directory.

What I'd like to do is change urls that look like this:

http://mydomain.com/index.php?a=xxxxxxxxxx

These urls will always have "index.php?a=". I have a different/new site that also has an index.php file, so it's important that I do a rewrite only when a= is in the URL.

The new url should be like

http://mydomain.com/ns1/index.php?a=xxxxxxxxxxx

Seems pretty simple, but i can't seem to get mod_rewrite to do it for me, here's what I have:

# rewrite old urls to new namespace
RewriteRule ^/index.php\?a=(.*)$ /gc1/index.php\?x=1&a=$1 [R=301,L]

See anything wrong?

+1  A: 

You can use this rule to add a prefix to the path:

RewriteRule !^ns1/ /ns1%{REQUEST_URI} [L]

This rule prefixes the URI path with /ns1 if the path is not already starting with it.

Gumbo
A: 

I believe that RewriteRule is only valid for paths, (I.e. it won't take into account any query parameters that you add.)

You are probably better off doing this in the PHP file itself.

// On old site
if($_GET['a']=="xxxxxxx"){
header("Location: /ns1/index.php?a=".$_GET['a']);
die();
}
webdestroya
A: 

Thanks to both of you.

I was able to do it:

# rewrite old urls to new ns1 namespace
RewriteCond %{QUERY_STRING} ^a=.*$
RewriteCond %{REQUEST_URI} !^/ns1/.*$
RewriteRule ^(.*)$ ns1$1 [R=301,L]
rcourtna