使用.htaccess更改正在显示的URL,但实际上没有重定向内容?

时间:2012-07-09 19:55:00

标签: .htaccess mod-rewrite proxypass

我有一个RewriteRule设置来改变

http://kn3rdmeister.com/blog/post.php?y=2012&m=07&d=04&id=4

进入

http://kn3rdmeister.com/blog/2012/07/04/4.php

但实际上重定向了浏览器从中获取页面的位置。我还想显示

/blog/post.php?y=xxxx&m=xx&d=xx&id=xx

但让浏览器显示更简单的网址,如

/blog/post/year/month/day/id.php

我在某处读到有关使用ProxyPass的内容,但我不太清楚我在做什么:P

我希望人们能够访问带有查询字符串的post.php URL,或者带有日期的花式shmancy子目录的干净URL,并获得相同的内容 - 所有这些都在最后显示干净的URL。 / p>

1 个答案:

答案 0 :(得分:2)

你想要这样的东西:

# First if someone actually requests a /blog/post.php URL, redirect them
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /blog/post\.php\?y=([0-9]{4})&m=([0-9]{2})&d=([0-9]{2})&id=([0-9]*)\ HTTP/
RewriteRule ^blog/post\.php$ /blog/%2/%3/%4/%5.php [R=301,L]

这会将浏览器重定向到/blog/##/##/##/##.php URI,该URI将显示在其地址栏中。重定向后,浏览器会发送/blog/##/##/##/##.php请求,然后您的服务器需要在内部重写 返回

# We got pretty URLs, but need to rewrite back to the php request
RewriteRule ^blog/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^\.]+)\.php$ /blog/post.php?y=$1&m=$2&d=$3&id=$4 [L]

这会在内部更改所有内容,以便文件/blog/post.php可以处理请求。