no-www

WWW to non-WWW Redirect with PHP

本小妞迷上赌 提交于 2019-11-26 13:03:45
问题 I want to redirect all www.domain.com requests to domain.com with PHP, basically: if (substr($_SERVER[\'SERVER_NAME\'], 0, 4) === \'www.\') { header(\'Location: http://\' . substr($_SERVER[\'SERVER_NAME\'], 4)); exit(); } However I do want to maintain the requested URL like in SO, for e.g.: http://www.stackoverflow.com/questions/tagged/php?foo=bar Should redirect to: http://stackoverflow.com/questions/tagged/php?foo=bar I don\'t want to rely on .htaccess solutions, and I\'m unsure which $

apache redirect from non www to www

ε祈祈猫儿з 提交于 2019-11-26 01:23:33
问题 I have a website that doesn\'t seem to redirect from non-www to www. My Apache configuration is as follows: RewriteEngine On ### re-direct to www RewriteCond %{http_host} !^www.example.com [nc] RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc] What am I missing? 回答1: Using the rewrite engine is a pretty heavyweight way to solve this problem. Here is a simpler solution: <VirtualHost *:80> ServerName example.com Redirect permanent / http://www.example.com/ </VirtualHost> <VirtualHost *:80

Nginx no-www to www and www to no-www

妖精的绣舞 提交于 2019-11-26 00:10:08
问题 I am using nginx on Rackspace cloud following a tutorial and having searched the net and so far can\'t get this sorted. I want www.mysite.com to go to mysite.com as normal in .htaccess for SEO and other reasons. My /etc/nginx/sites-available/www.example.com.vhost config: server { listen 80; server_name www.example.com example.com; root /var/www/www.example.com/web; if ($http_host != \"www.example.com\") { rewrite ^ http://example.com$request_uri permanent; } I have also tried server { listen