0

我正在使用以下 htaccess 文件来实现一些重定向等:

RewriteEngine On

Options +FollowSymLinks 
RewriteEngine on

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

RewriteCond %{HTTPS} !=on
RewriteRule .* https://www.myurl.com/$1 [R,L]

RewriteRule ^$ /home [R=301,L]

最近我添加了最新的一行,所以当用户访问https://www.myurl.com时,他会被重定向到https://www.myurl.com/home的主页。这可以正常工作,但我有一种不好的感觉,应该以某种方式更好地编写。我不喜欢 ^$ 部分,但它有效。我怎么能重写这个?

4

1 回答 1

1

HTTPS部分绝对可以重写,并且在您创建多个重定向时应该首先出现。

RewriteEngine On

Options +FollowSymlinks 
RewriteBase /

#redirect all traffic looking for a domain not starting with www &
#all non-https traffic to the https://www domain
RewriteCond %{HTTPS} off
#This condition is better for analytics tracking and SEO (single subdns)
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteRule ^index\.php$ - [L]

RewriteRule ^$ home [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

由于您的 MVC 可能使用 index.php 来处理所有请求,因此重定向/home可能无法正常工作,除非您在不请求任何文件的情况下访问该网站(仅限根目录)。如果有人明确要求https://www.myurl.com/index.php您的规则RewriteRule ^index\.php$ - [L]告诉网站不要更改任何内容。既然你说这按预期工作,我没有改变订单。

如果它没有按预期工作,则将规则的顺序切换为/home.

RewriteRule ^$ home [R=301,L]
RewriteRule ^index\.php$ - [L]
于 2013-05-27T20:05:06.660 回答