2012-11-09 53 views
0

这是我第一次尝试编写htaccess规则。我的目标是使所有相关链接重定向到绝对。首先,我是测试:htaccess RewriteRule页面没有正确重定向

RewriteEngine On 

RewriteRule gallery\.php$ http://www.domain.com/sample/gallery.php 
RewriteRule info\.php$ http://www.domain.com/sample/gallery.php 

的第一条规则让火狐抛出错误“页面无法正常重定向”当我点击链接,而第二条规则正常工作。对于未来的想法是写一个规则一样,

RewriteRule catchAllRelativeLinks$ http: //www.domain.com/sample/$1 

,但如果我不能做的第一个规则的工作,我不认为我会找到如何让真正的规则。

编辑: 避免无限循环我不能试图通过捕捉变量来了解我是第一次还是第二次?一些想法,我试图(和zhcon失败):

RewriteCond %{IS_SUBREQ} false 
RewriteRule ^gallery\.php$ http://www.domain.com/gallery.php?a [R=302,L] 

RewriteCond %{THE_REQUEST} !(\?something)$ 
RewriteRule ^gallery\.php$ http://www.domain.com/gallery.php?something [R=302,L] 

或使用环境变量,

再次感谢

+0

什么是第一个旨在匹配是它表示为'/ gallery.php'重定向到'/样品/ gallery.php' ?如果是这样,它应该包括'^',如在'^ gallery \ .php $' –

+0

U可以检出这个解释和指南相同(:http://stackoverflow.com/a/21754308/5465790 – Sonu

回答

1

第一条规则,你必须在一个无限循环的结果。 mod_rewrite并不是绝对的绝对相对链接。这应该在HTML中完成。例如,打开

<a href="gallery.php">Gallery</a> 

<a href="/sample/gallery.php">Gallery</a> 

然而,第二条规则的作品,因为它做一些事情mod_rewrite确实非常好,而这从一个网页或网址到另一个重定向。欲了解更多信息,请参阅Apache documentation

+0

为了避免无尽的循环无法捕捉IS SUBREQ或抛出并捕获一个参数吗?我编辑了这个问题。 – Riccardo

+0

是的,它似乎只能重定向一次。 – Geremia

0

您可以通过此链接查看详细说明。

https://wiki.apache.org/httpd/RewriteHTTPToHTTPS

这为我工作(?:

 

    RewriteEngine on 

    # Check for POST Submission | 
    # Because POST parameters aren't retained on a redirect. 
    # You can omit that line if you want to make sure that all POST submissions are secure 
    # (any unsecured POST submissions will be ignored) 
    RewriteCond %{REQUEST_METHOD} !^POST$ 

    # Forcing HTTPS 
    RewriteCond %{HTTPS} !=on [OR] 
    RewriteCond %{SERVER_PORT} 80 
    # Pages to Apply 
    RewriteCond %{REQUEST_URI} ^something_secure [OR] 
    RewriteCond %{REQUEST_URI} ^something_else_secure 
    RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]