重写条件重定向而不是重写 [英] rewrite condition redirects instead of rewriting
问题描述
我正在尝试将我的URL http://givehands.co.in/post.php?p=post&id=1
重写为使用htacess http://givehands.co.in/post/1/
。
Iam trying to rewrite my URL http://givehands.co.in/post.php?p=post&id=1
to http://givehands.co.in/post/1/
using htacess.
但是它被重定向到URL http://givehands.co.in/post.php?p=post&id=1
而不是重写它?我的域赠手已映射到Web服务器的子目录。
But it gets redirected to the URL http://givehands.co.in/post.php?p=post&id=1
instead of rewriting it? My domain givehands is mapped to a sub directory of my webserver.
有人可以帮助我解决正确的重写规则吗?
Could any one help me out with the correct rewrite rule? Iam new to this.
Options -Multiviews
RewriteEngine On # Turn on the rewriting engine
RewriteBase /
RewriteCond %{REQUEST_URI} post/(.*)
RewriteRule post/(.*)/ http://givehands.co.in/post.php?p=post&id=$1 [L]
RewriteCond %{REQUEST_URI} post/
RewriteRule post/ http://givehands.co.in/post.php [L]
推荐答案
您的重定向规则中似乎有完整的URL:
It looks like you have a full URL in your redirect rule:
RewriteCond %{REQUEST_URI} post/
RewriteRule post/ http://givehands.co.in/post.php [L]
在其中输入完整的URL会强制Apache重定向浏览器,而不是重写它:
https://httpd.apache.org/docs/current/mod/mod_rewrite.html
Putting a full URL in there forces Apache to redirect the browser instead of rewriting it: https://httpd.apache.org/docs/current/mod/mod_rewrite.html
绝对URL
如果指定了绝对URL,则mod_rewrite检查主机名是否与当前主机匹配。如果是这样,将删除
方案和主机名,并将生成的路径视为
作为URL路径。否则,将对
给定的URL执行外部重定向。要强制外部重定向回到当前主机,请在下面的[R]标志中查看
。
Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.
如果Givehands.co .in是您托管的域,请尝试将其更改为:
If givehands.co.in is the domain you're hosting, try changing it to:
RewriteCond %{REQUEST_URI} post/
RewriteRule post/ /post.php [L]
如果Givehands.co.in不在您的计算机上服务器或位于其他VirtualHost下的服务器,则无法†来重写 URL,您必须重定向浏览器(这就是Apache率先为您执行此操作的原因)。
If givehands.co.in isn't on your server, or is under a different VirtualHost, then there's no way† to "rewrite" the URL, you have to redirect the browser (which is why Apache is taking the lead and doing this for you).
†实际上,您可以设置反向代理来执行类似的操作,但这可能是另一个讨论的主题:)
† You could actually setup a reverse proxy to do something like this, but that's probably a topic for a different discussion :)
这篇关于重写条件重定向而不是重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!