htaccess将除一个网址之外的所有网址重定向到不同的网域

时间:2017-10-11 10:56:59

标签: apache .htaccess redirect

我正在尝试将 http://shop.mysite.com/enquiry/之外的所有网址重定向到下面的其他域名,但它会将所有内容重定向到“example.com”,包括“查询”页面。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/enquiry/
RewriteRule .* https://www.example.com [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^(.*)$ index.php
</IfModule>

那么如何确保“查询”不被重定向?

2 个答案:

答案 0 :(得分:1)

对于否定条件,您应该 public void DownloadFoo() { var webClient = new WebClient(); var totalBytes = 0l; var destFile = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "platform-tools-latest-windows.zip")); webClient.DownloadProgressChanged += (s, e) => Debug.WriteLine($"Download progress changed: { e.ProgressPercentage }% ({ e.BytesReceived } / { (totalBytes = e.TotalBytesToReceive) })"); using (webClient) { var buffer = webClient.DownloadData(new Uri("https://dl.google.com/android/repository/platform-tools-latest-windows.zip")); using (var oStream = destFile.Open(FileMode.Truncate)) { oStream.Write(buffer, 0, buffer.Length); oStream.Flush(true); // true => flushToDisk } } // webClient is automatically disposed of; method will return cleanly } 变量而不是THE_REQUEST

REQUEST_URI

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{THE_REQUEST} !/enquiry[?\s/] [NC] RewriteRule ^ https://www.example.com [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}/index.html !-f RewriteCond %{REQUEST_FILENAME}/index.php !-f RewriteRule ^ index.php [L] </IfModule> 变量表示Apache从您的浏览器收到的原始请求,并且在执行某些重写规则后没有被覆盖。此变量的示例值为THE_REQUEST

确保在测试时清除浏览器缓存或使用新浏览器。

答案 1 :(得分:1)

index.php的最终RewriteRule可能导致PHP脚本中出现额外的重定向。

您可以尝试移动此规则下方的重定向:

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}/index.html !-f RewriteCond %{REQUEST_FILENAME}/index.php !-f RewriteRule ^(.*)$ index.php RewriteCond %{REQUEST_URI} !^/enquiry/? RewriteRule .* https://www.example.com [R=301,L] </IfModule>

此外,您还需要添加一些重写条件,以排除引用的脚本/样式表被重定向。