0

嗨,我的 .htaccess 文件规则有点卡住了。

我已经设置了非 https 重定向到 https(这有效):

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

我已经将非 www 设置为 www (这有效):

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

我已经设置,所以它总是在 url 中添加斜杠(这会在thank you.php 中添加斜杠):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]

重定向(不起作用):

RewriteRule ^thank-you/?$ thank-you.php [L]
RewriteRule ^thank-you.php$ http://%{HTTP_HOST}/thank-you/ [L]

应该发生的情况是,如果有人转到任何不是 www 或不是 http 的地址,他们将被重定向到 https 和 www。

当文件thank-you.php被访问时,它应该重定向到/thank-you/

如果有人访问 /thank-you,它应该重定向到 /thank-you/

由于 URL 重写不起作用,但看起来应该如此,因此有点卡住了。

提前致谢

完整代码:

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]

RewriteRule ^thank-you/?$ thank-you.php [L]
RewriteRule ^thank-you.php$ http://%{HTTP_HOST}/thank-you/ [L]

现在奇怪的是这段代码将 /thank-you.php 重定向到 /thank-you/ 但没有将 /thank-you 重定向到 /thank-you/ 我认为有些冲突:

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]

RewriteRule ^thank-you/?$ thank-you.php [L]
RewriteRule ^thank-you.php$ http://%{HTTP_HOST}/thank-you/ [L]
4

1 回答 1

0

我用波纹管解决了我的问题:

访问:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] 

php:

function CheckUrl ($s) { // Get the current URL without the query string, with the initial slash
$myurl = preg_replace ('/\?.*$/', '', $_SERVER['REQUEST_URI']); //If it is not the same as the desired URL, then redirect
if ($myurl != "/$s") {Header ("Location: /$s", true, 301); exit;}
}

$producturl = "thank-you/";
CheckUrl ($producturl); //redirects the user if they are at the wrong place
于 2013-02-15T11:28:05.900 回答