Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

apache - htaccess rewriterule and redirect not working

I have a particular scenario: the old site urls were made this way: http://www.mysite.it/it/my-old-link

The new one are made this way: http://www.mysite.it/vini.php?famiglia=my-new-link

I would like to the link http://www.mysite.it/it/my-old-link to point to http://www.mysite.it/vini.php?famiglia=my-new-link

This is to preserve SEO position. So, when someone click on search result http://www.mysite.it/it/my-old-link will be redirected to http://www.mysite.it/vini.php?famiglia=my-new-link

But I cannot find a way to do this editing the .htaccess file.

I've set a rewrite rule and a redirect but it doesn't work; here an example of the .htaccess file:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /

RewriteRule ^/?([^/d]+)/?$ /vini.php?famiglia=$1 [L,QSA]

Redirect 302 http://www.mysite.it/it/my-old-link http://www.mysite.it/my-new-link

ErrorDocument 404 http://www.mysite.it/index.php

But it doesn't work: if I enter http://www.mysite.it/it/my-old-link in the browser I get the 404 page error (index.php)

However: if I enter http://www.mysite.it/my-new-link on the browser I get the correct page.

Is there a way to solve this problem?

TIA tony


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In case you are looking for specific rule as per shown samples then try following.

RewriteEngine ON
RewriteRule ^it/my-old-link/?$ vini.php?famiglia=my-new-link [NC,L]


In case you are looking for Generic rules, where if a URI is NOT present as a file or directory then try following. Please make sure either you put above or following rules only at a time.

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ vini.php?famiglia=my-new-link [NC,L]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...