2017-01-02 57 views
1

我正在运行一个使用nginx和laravel框架的站点,并且我不知道如何让我的url始终在我的url中包含http://www。目前它只是domain.com,但我希望它是http://www.domain.com如何在我的url中始终包含http:// www

如果我使用www.domain.com或domain.com它的作品,但domain.com将不会重定向到www.domain.com。

我也想总是在我的网址中包含http://。

我需要改变这是nginx,laravel还是我的.htaccess文件,以及我需要做什么更改。这是我的nginx /网站可用的默认文件

## 
# You should look at the following URL's in order to grasp a solid understanding 
# of Nginx configuration files in order to fully unleash the power of Nginx. 
# http://wiki.nginx.org/Pitfalls 
# http://wiki.nginx.org/QuickStart 
# http://wiki.nginx.org/Configuration 
# 
# Generally, you will want to move this file somewhere, and start with a clean 
# file but keep this around for reference. Or just disable in sites-enabled. 
# 
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. 
## 

# Default server configuration 
# 
server { 
     listen 80 default_server; 
     listen [::]:80 default_server; 

     # SSL configuration 
     # 
     # listen 443 ssl default_server; 
     # listen [::]:443 ssl default_server; 
     # 
     # Note: You should disable gzip for SSL traffic. 
     # See: https://bugs.debian.org/773332 
     # 
     # Read up on ssl_ciphers to ensure a secure configuration. 
     # See: https://bugs.debian.org/765782 
     # 
     # Self signed certs generated by the ssl-cert package 
     # Don't use them in a production server! 
     # 
     # include snippets/snakeoil.conf; 

     root /var/www/laravel/public; 

     # Add index.php to the list if you are using PHP 
     index index.php index.html index.htm; 

     server_name domain.com; 

     location/{ 
       # First attempt to serve request as file, then 
       # as directory, then fall back to displaying a 404. 
       try_files $uri $uri/ /index.php?$query_string; 
     } 

     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
     # 


     # 
     #  # With php7.0-cgi alone: 
     #  fastcgi_pass 127.0.0.1:9000; 
     #  # With php7.0-fpm: 
       fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
     } 

     # deny access to .htaccess files, if Apache's document root 
     # concurs with nginx's one 
     # 
     location ~ /\.ht { 
       deny all; 
     } 
} 


# Virtual Host configuration for example.com 
# 
# You can move that to a different file under sites-available/ and symlink that 
# to sites-enabled/ to enable it. 
# 
#server { 
#  listen 80; 
#  listen [::]:80; 
# 
#  server_name example.com; 
# 
#  root /var/www/example.com; 
#  index index.html; 
# 
#  location/{ 
#    try_files $uri $uri/ =404; 
#  } 
#} 

,这是我的.htaccess

<IfModule mod_rewrite.c> 
    <IfModule mod_negotiation.c> 
     Options -MultiViews 
    </IfModule> 

    # Redirect Trailing Slashes If Not A Folder... 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)/$ /$1 [L,R=301] 

    # Handle Front Controller... 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule^index.php [L] 

    # Handle Authorization Header 
    RewriteCond %{HTTP:Authorization} . 
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 
</IfModule> 

nginx.conf文件:

user www-data; 
worker_processes auto; 
pid /run/nginx.pid; 

events { 
     worker_connections 768; 
     # multi_accept on; 
} 

http { 

     ## 
     # Basic Settings 
     ## 
     client_max_body_size 20M; 
     sendfile on; 
     tcp_nopush on; 
     tcp_nodelay on; 
     keepalive_timeout 65; 
     types_hash_max_size 2048; 
     # server_tokens off; 

     # server_names_hash_bucket_size 64; 
     # server_name_in_redirect off; 

     server { 
      listen  80; 
      server_name www.example.org example.org; 
      if ($http_host = example.org) { 
       rewrite (.*) http://www.example.org$1; 
     } 
     include /etc/nginx/mime.types; 
     default_type application/octet-stream; 

     ## 
     # SSL Settings 

     ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE 
     ssl_prefer_server_ciphers on; 

     ## 
     # Logging Settings 
     ## 

     access_log /var/log/nginx/access.log; 
     error_log /var/log/nginx/error.log; 

     ## 
     # Gzip Settings 
     ## 

     gzip on; 
     gzip_disable "msie6"; 

     # gzip_vary on; 
     # gzip_proxied any; 
     # gzip_comp_level 6; 
     # gzip_buffers 16 8k; 
     # gzip_http_version 1.1; 
     # gzip_types text/plain text/css application/json application/javascript text/xml applicati$ 

     ## 
     # Virtual Host Configs 
     ## 

     include /etc/nginx/conf.d/*.conf; 
     include /etc/nginx/sites-enabled/*; 
} 


#mail { 
#  # See sample authentication script at: 
#  # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript 
# 
#  # auth_http localhost/auth.php; 
#  # pop3_capabilities "TOP" "USER"; 
#  # imap_capabilities "IMAP4rev1" "UIDPLUS"; 
# 
#  server { 
#    listen  localhost:110; 
#    protocol pop3; 
#    proxy  on; 
#  } 
# 
#  server { 
#    listen  localhost:143; 
#    protocol imap; 
#    proxy  on; 
#  } 
#} 
+0

你使用apache2 + nginx?或者其中之一? –

回答

0

你需要使用301重定向https://en.wikipedia.org/wiki/HTTP_301

nginx指令:

server { 
    listen  80; 
    server_name www.example.org example.org; 
    if ($http_host = example.org) { 
     rewrite (.*) http://www.example.org$1; 
    } 
    ... 
} 

指令的Apache:

RewriteCond %{HTTP_HOST} example.org 
RewriteRule (.*)   http://www.example.org$1 

http://nginx.org/en/docs/http/converting_rewrite_rules.html

+0

我正在使用nginx。这不适合我的工作。我只是将代码粘贴到/etc/nginx/nginx.conf中,并将example.org替换为我的域名并重新启动nginx,对吗? – Feltie

+0

知道它正确重定向,但现在我只是得到欢迎使用nginx!屏幕 – Feltie

+0

显示您的完整配置 –

0

可以使用.htaccess文件添加它。

你必须写重写规则在您的.htaccess文件如下

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

在.htaccess文件添加以下代码和你做。

相关问题