我读了很多答案,但似乎没有什么对我有用。我想将非www域重定向到www,但这并没有发生。
我的.htaccess文件在/home/aashh/mywebsite中,我的网站代码位于:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^getechready.com [NC]
RewriteRule ^(.*)$ http://www.getechready.com/$1 [L,R=301]
mywebsite.conf/etc/apache2/可用站点中的文件:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/ashish/mywebsite/static
<Directory /home/ashish/mywebsite/static>
Require all granted
</Directory>
Alias /media /home/ashish/mywebsite/media
<Directory /home/ashish/mywebsite/media>
Require all granted
</Directory>
<Directory /home/ashish/mywebsite/mywebsite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /home/ashish/mywebsite>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
WSGIScriptAlias / /home/ashish/mywebsite/mywebsite/wsgi.py
WSGIDaemonProcess django_app python-path=/home/ashish/mywebsite python->
WSGIProcessGroup django_app
</VirtualHost>
apache2.conf在/etc/apache2:中
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^.ht">
Require all denied
</FilesMatch>
当我在.htaccess文件网站上写一些胡言乱语时,会出现500个错误。所以.htaccess文件正在被读取,但当我执行curl-I getechready.com时,它显示url未被解析,浏览器中也不会发生重定向。
我使用的是Python 3.8和django 3.1。
注意:我的文档根目录是/var/www/html,但我的网站代码位于/ash/home/mywebsite,我所在的位置。htaccess文件
如果需要任何其他数据,请告诉我。
我没有从apache端得到任何适合我的答案,所以,因为我使用的是Django,所以我在settings.py文件中添加了PREPEND_WWW = True
,只需确保您的中间件中有'django.middleware.common.CommonMiddleware',
,并记住从.htaccess文件中删除所有规则。为我工作