Skip to content

Instantly share code, notes, and snippets.

@lenivene
Last active February 8, 2016 14:20
Show Gist options
  • Save lenivene/d0a5eef9fd12590ac825 to your computer and use it in GitHub Desktop.
Save lenivene/d0a5eef9fd12590ac825 to your computer and use it in GitHub Desktop.
Force loading with the WWW ( htaccess, php, nginx and web.config )
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
<?php
function get_current_url(){
$url = 'http';
if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on'){
$url .= s;
}
$url .= '://www.';
if ( isset( $_SERVER['SERVER_PORT'] ) && $_SERVER['SERVER_PORT'] != 80 ) {
$url .= $_SERVER['SERVER_NAME'] . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
}else{
$url .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
}
/*
* Is not required
*/
//$url = str_replace( "'", '', $url );
//$url = str_replace( '"', '', $url );
return $url;
}
if ( strpos( $_SERVER['SERVER_NAME'], 'www.') === false ){
@header( "Location: ". get_current_url(), true, 301 );
exit();
}
echo "Hello Word";
location / {
if ($http_host !~ "^$"){
rewrite ^(.*)$ http%1://www.$http_host$request_uri redirect;
}
}
<rule name="rule 1f" stopProcessing="true">
<match url="^" />
<action type="Rewrite" url="/http%1://www.%{HTTP_HOST}%{REQUEST_URI}" />
</rule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment