欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php url 跳转老目录到新目录

程序员文章站 2022-05-30 20:20:59
...
一个关于手机的网站,用wordpress基板。
原先的htacess 放在主目录下 /var/www/html
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]

新建一个 .htacess 放在子目录下 `/var/www/html/2014·
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]


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


我尝试在items/products.php里把源代码修改为

居然报错 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.comRewriteEngine 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]