开发者

Help with mod_rewrite conditional statement

开发者 https://www.devze.com 2023-03-25 09:49 出处:网络
I have updated my site\'s SEO friendly URLs and submitted a new site map to Google. The problem is that I seem to be getting punished in rankings for having my old content still showup under the old U

I have updated my site's SEO friendly URLs and submitted a new site map to Google. The problem is that I seem to be getting punished in rankings for having my old content still show up under the old URL as well as the new URL (the one in the site map). I have been trying to figure out how to capture the variables with regex to make the following work but cannot seem to get it right.

Any ideas? We have an automotive classifieds that has been affected by the panda/farmer update.

Old URL:  http://www.mysite.com/forsale/light-trucks/12345/2010-dodge-ram-1500.html

New URL:  http://www.mysite.com/cars-trucks-for-sale/light-trucks/2010/dodge/ram-1500/12345/2010-dodge-ram-1500.html

Right now, either one of those URLs will work (they rewrite to a s开发者_高级运维cript: showcontent.php?id=12345).

This means duplicate content since Google still knows about my old URL and my site maps all show only the new URL...

I need to show a a 301 redirect and make sure that my old content gets rewritten to the new format on the fly but notice that I have some NEW url parameters that are not in the old url...

RewriteRule ^/?(forsale)/([-a-zA-Z0-9_-]+)/([0-9]+)/([-a-zA-Z0-9_-]+)\.html$ showcontent.php?id=$3 [L]

RewriteRule ^/?(cars-trucks-for-sale)/([-a-zA-Z0-9_-]+)/([0-9]+)/([-a-zA-Z0-9_-]+)/([-a-zA-Z0-9_-]+)/([0-9]+)/([-a-zA-Z0-9_-]+)\.html$ showcontent.php?id=$6 [L]

The above rules work now but I need to capture inbound requests to the first one and rewrite them to the second. I could just stop honouring the original URL but I had some rank on those and I don't want to just show a 401 error.


As I understand content-type and new-content-type are constant values and that is the ONLY difference between old and new URLs.

If so -- there are quite a few ways of doing this:

1. Using mod_rewrite (you asked for it -- place these lines in .htaccess in website root folder. If placed elsewhere some tweaking will be required):

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteRule ^content-type/([^/]+)/(\d+)/([^/]+\.html)$ http://www.mysite.com/new-content-type/$1/$2/$3 [R=301,L]

Oh well -- since URLs are very similar (based on the examples and description you have provided) -- you can go even this simpler way (NOTE: this will redirect ANY URLs in /content-type/ folder. If your aim is to redirect only .html ended URLs that match that structure, then do not use it):

RewriteRule ^content-type/(.+)$ http://www.mysite.com/new-content-type/$1 [R=301,L]

2. Not using mod_rewrite -- URL structure of old and new URLs is very similar .. so even this will do the job (see the NOTE above):

Redirect 301 /content-type/ http://www.mysite.com/new-content-type/

This will redirect (301 Permanent Redirect) all requests to URL that starts with /content-type/ to exactly the same but with /new-content-type/ instead.


UPDATE: more real URLs provided by OP

Because your REAL URLs are quite different from old ones, there is no really good way to write a single line rewrite rule for this. Therefore I see 2 major options:

1. Create redirect rule for EACH old URL (place it somewhere on the top of .htaccess, preferably in server config, if possible):

Redirect 301 /forsale/light-trucks/12345/2010-dodge-ram-1500.html http://www.mysite.com/cars-trucks-for-sale/light-trucks/2010/dodge/ram-1500/12345/2010-dodge-ram-1500.html

If you have tons of such old URLs .. then this approach is not really good -- too many URLs to test on every single request + cost of maintenance (create these redirect rules).

2. Keep using current redirect rules that you have in place (the one that rewrites to showcontent.php?id=12345). In this showcontent.php file generate proper URL (as it should be on a website) and compare to requested URL (take it from $_SERVER['REQUEST_URI']). If URLs are different then issue 301 redirect to a proper URL (using header() function). -- This is how THIS website works, for example. It may be a bit slower than .htaccess (as PHP has to kick in) .. but MUCH easier to implement and maintain (virtually zero maintenance cost).

0

精彩评论

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