[英]htaccess Redirect problem with internal pages
When I type http://mywebsite.com my server redirects successfully to http://www.mywebsite.com but when I type http://mywebsite.com/page.php it redirects to http://www.mywebsite.com not to http://www.mywebsite.com/page.php . 当我键入http://mywebsite.com时,我的服务器成功重定向到http://www.mywebsite.com,但是当我键入http://mywebsite.com/page.php时,它重定向到了http://www.mywebsite。 com不要访问http://www.mywebsite.com/page.php 。
My server is Apache, I use these lines to redirect from non-www to www in my .htaccess 我的服务器是Apache,我使用这些行将.htaccess中的非www重定向到www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(.*)\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R=301,L]
Thanks 谢谢
Try it like this: 像这样尝试:
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Or to redirect ot HTTPS: 或重定向ot HTTPS:
# HTTP OVER SSL
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I would suggest the following: 我建议以下内容:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mywebsite\.com [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R=301,L]
I think your condition was being too strict. 我认为您的条件太严格了。 You were ending it (
$
) too soon. 您即将结束(
$
)。
I'd rather use this if you want to add a www
at the start of your url if none exists : 如果您不希望在网址的开头添加
www
,那么我更愿意使用它:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
I do not recommend redirecting users to the in the .htaccess. 我不建议将用户重定向到.htaccess中。 You should do that in the config file for the virtual host if possible, using a temporary redirect, or permanent if appropriate, for visitors coming from a non http port (eg 80).
如果可能,您应该在虚拟主机的配置文件中执行此操作,对于来自非http端口(例如80)的访问者,使用临时重定向或适当时使用永久重定向。
It might look something like this: 它可能看起来像这样:
<VirtualHost *:80>
ServerName mywebsite.com
ServerAlias www.mywebsite.com
DocumentRoot "/var/www/mywebsite.com"
Redirect / https://www.mywebsite.com/
</VirtualHost>
<VirtualHost *:443>
ServerName mywebsite.com
DocumentRoot "/var/www/mywebsite.com"
Redirect / https://www.mywebsite.com/
... SSL stuff
.... your https config for mywebsite here, with SSLCertificate, I believe you need a separate one for without www, to redirect to www.
</VirtualHost>
<VirtualHost *:443>
ServerName www.mywebsite.com
DocumentRoot "/var/www/mywebsite.com"
SSL STUFF****
</VirtualHost>
Then in the .htaccess you should not need any rewriting at all. 然后,在.htaccess中,您根本不需要任何重写。
I believe the problem with the other answers, is that you were rewriting the location for a file, when it should be in the format of %{DOCUMENT_ROOT}%{REQUEST_URI}, so you might have an error handler sending you back to the home page. 我认为其他答案的问题是,您正在重写文件的位置,而该文件的格式应为%{DOCUMENT_ROOT}%{REQUEST_URI},因此您可能会有错误处理程序将您发送回首页页。 So check your error logs and access logs.
因此,请检查您的错误日志和访问日志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.