Maison >développement back-end >tutoriel php >php url 跳转老目录到新目录

php url 跳转老目录到新目录

WBOY
WBOYoriginal
2016-06-23 13:52:051575parcourir

一个关于手机的网站,用wordpress基板。
原先的htacess 放在主目录下 /var/www/html

<IfModule mod_rewrite.c>RewriteEngine OnRewriteBase /RewriteRule ^items/products/(\d+)/(.*)?$ items/products.php?number=$1&name=$2 [QSA,L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]</IfModule>

新建一个 .htacess 放在子目录下 `/var/www/html/2014·
<IfModule mod_rewrite.c>RewriteEngine OnRewriteBase /2014/RewriteRule ^phone/products/(\d+)/(.*)?$ phone/products.php?number=$1&name=$2 [QSA,L]RewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-fRewriteRule (.*) $1.php [L]</IfModule>


现在要把旧的URL地址永久跳转到新的URL地址里
www.example.com/items/products/1234/iphone-5c 
=> 
www.example.com/2014/phone/products/1234/iphone-5c


我尝试在items/products.php里把源代码修改为
<?phpheader("Status: 301 Moved Permanently");header('http://www.example.com/2014/phone/products/'.$_GET['number'].'/'.$_GET['name']);exit;?>

居然报错 Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

把源代码改回原来的,添加 echo 'http://www.example.com/2014/phone/products/'.$_GET['number'].'/'.$_GET['name'];
页面正常显示http://www.example.com/2014/phone/products/1234/iphone-5c,鼠标全选右键打开URL在新窗口,可以正常跳转,但为什么apache回报错?

然后再尝试修改

然后主目录下的那个htacess
RewriteRule ^item/products/(\d+)/(.*)?$ items/products.php?number=$1&name=$2 [QSA,L]
变成
RewriteRule ^2014/phone/products/(\d+)/(.*)?$ items/products.php?number=$1&name=$2 [QSA,L,R=301]

这次居然跳转到wordpress的 404 页面了。

郁闷,到底要怎么做,才能正常跳转?


回复讨论(解决方案)

1)header跳转之所以报错,写法错误。header("Location:$url ");
另外里面的$_GET这几个如果不存在,又没有设置报错级别,那么还是容易报错

2)重写文件.htaccess 无需放到 /2014/目录,直接在原来的 / 重写即可

#by default7#zbphp.com<IfModule mod_rewrite.c>RewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^items/products/(\d+)/(.+)?$  2014/phone/products/$1/$2/  [L,R=301]RewriteRule ^2014/phone/products/(\d+)/(.+)?$ phone/products.php?number=$1&name=$2 [PT,QSA,L]RewriteRule (.*) $1.php [L]</IfModule>

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn