的.htaccess重写GET变量W / 301重定向 [英] .htaccess rewrite GET variables w/ 301 redirect
问题描述
我试图同时做301重定向,并通过剥离获得变量imporve我的URL结构。
I'm trying to simultaneously do a 301 redirect and imporve my url structure by stripping the get variables.
我最近更新我的网站,谷歌有一些旧的网页缓存有因为感动。该结构看起来像这样
I recently updated my website and Google has some old pages cached that have since moved. The structure looks like this
wwww.domain.com/locations.asp?zip=10001 <---OLD URL
wwww.domain.com/locations/?zip=10001 <---NEW URL
现在我用下面一行在我的.htaccess文件我重定向页面旧到新的:
Right now I'm redirecting the old page to the new using the following line in my .htaccess file:
Redirect 301 /solar_panel_systems_zip.asp /zip-code/
以上工作正常,但我想的网址如下:
The above works fine but I'd like the URL to be as follows:
wwww.domain.com/locations/zip/10001
我碰到这个职位的.htaccess重写GET变量并尝试这个规则,但没有运气:/
I came across this post .htaccess rewrite GET variables and tried this rule but no luck :/
RewriteRule ^([\w\d~%.:_\-]+)$ index.php?page=$1 [NC]
我假设这是因为我是301重定向,做一个重写?
I'm assuming this is because I'm 301 redirecting and doing a rewrite?
推荐答案
您希望这样的事情在你的htaccess文件:
You want something like this in your htaccess file:
RewriteRule ^locations/zip/([0-9]+)$ /locations/?zip=$1 [L,QSA]
这将使它所以当有人请求 http://www.domain.com/locations/zip/10001
,他们将得到服务<$ C的内容$ C> /locations.php?zip=10001 。
This will make it so when someone requests http://www.domain.com/locations/zip/10001
, they will get served the contents of /locations.php?zip=10001
.
要重定向的旧网址到新的,你还需要添加:
To redirect the old URLs to the new ones, you will need to also add:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /locations(\.asp)?/?\?zip=([0-9]+)&?([^\ ]*)
RewriteRule ^ /locations/zip/%3?%4 [L,R=301]
所以,当有人试图去 http://www.domain.com/locations.asp?zip=10001
或 http://www.domain.com/locations/?zip=10001
,他们重定向到 http://www.domain.com/locations/ ZIP / 10001
。
So when someone tries to go to http://www.domain.com/locations.asp?zip=10001
or http://www.domain.com/locations/?zip=10001
, they get redirected to http://www.domain.com/locations/zip/10001
.
这篇关于的.htaccess重写GET变量W / 301重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!