简体   繁体   中英

Redirect 301 / Rewrite conundrum

On my site, users can add various URL's that need to be redirected.

For example; from this: domain.com/oldpage/36/

To this: domain.com/newpage/47/

They are added to the .htaccess like this:

Redirect 301 /oldpage/36/ /new-page/47/

But when accessing the old page they get this:

domain.com/newpage/47/?pid=36&pagename=oldpage

I'm pretty sure these rewrite rules are causing this predicament:

RewriteRule ([^.]+)/([0-9]+)/$ index.php?pid=$2&pagename=$1
RewriteRule ([^.]+)/([0-9]+)/([^.]+) index.php?pid=$2&pagename=$1&vars=$3

However, mod_rewrite stuff is not my strongpoint, so I have no idea how to fix it.

Any ideas ?

Since you are already using mod_rewrite anyway, I suppose you should make your redirects using rewrites too

RewriteRule /oldpage/36/ /new-page/47/ [R=301]

This will "rewrite" the URL from old to new, and will redirect the browser to new url with status code 301 . [R] directive means redirect, which also stops other rules from processing, hence the other rules will be handled only when the new request is sent from broswer with new url.

Adding a ? makes the Rewrite not add the query string to the url.

so this should work:

Redirect 301 /oldpage/36/ /new-page/47/?

As a precaution you could also add it to the end of:

RewriteRule ([^.]+)/([0-9]+)/$ index.php?pid=$2&pagename=$1?
RewriteRule ([^.]+)/([0-9]+)/([^.]+) index.php?pid=$2&pagename=$1&vars=$3?

But only if they are needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM