开发者

mod_rewrite to change my urls

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=xxxxxxx开发者_如何学JAVAxxxx

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?


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.


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();
}


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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜