0

I am using the following htaccess code in my website:

Options +FollowSymLinks     
RewriteEngine on

RewriteRule ^([^/.]+)?/?([^/.]+)?/?$ switch.php?one=$1&two=$2 [L]

switch.php has a code that helps my dictionary website to present nice looking addresses.

Lets say that the word XXX in the dictionary is clicked, then the address will be: example.com/XXX

(all by using the same "page.php" file for all the words in the dictionary.

Another example, a user can write the address example.com/YYY and he will be directed to the YYY page which will use "page.php" file and be shown in the address-bar as "example.com/YYY.

My problem is that I want to strip off the www from all my pages but still use my code. so any www.example.com/XXX will be example.com/XXX

I tried many ways to do it but all the strip off www codes do not work well with my code. (I need the switch.php to keep working as I do not want to change the addresses of my website, just to remove www from the pages)

I appreciate anyone who could help me with a solution. I do not mind using 301 direct as well as long as the switch.php still works.

Thanks!

anubhava
  • 761,203
  • 64
  • 569
  • 643

1 Answers1

0

This might be what you're looking for. Or it's at least close. This example strips the http://, www, and the slash at the end of the url, so you can tweak it if it's not quite right for you. It'll do something like this

Input - > http://www.google.com/ | Output -> google.com

$input = 'www.google.co.uk/';

// in case scheme relative URI is passed, e.g., //www.google.com/
$input = trim($input, '/');

// If scheme not included, prepend it
if (!preg_match('#^http(s)?://#', $input)) {
    $input = 'http://' . $input;
}

$urlParts = parse_url($input);

// remove www
$domain = preg_replace('/^www\./', '', $urlParts['host']);

echo $domain;

// output: google.co.uk
Community
  • 1
  • 1
T Martin
  • 3
  • 1
  • Thanks for the answer but I need help with EDITING THE CODE THAT I POSTED i.e to combine my code with the www-strip-off code. – Amichay Rab Jan 10 '15 at 17:42