Redirecting http://example.com to http:/www.example.com while keeping http://subdomain.example.com access intact

跟風遠走 提交于 2019-12-12 02:39:01

问题


I have a website on which I've enabled subdomain access such as:

 http://subdomain1.example.com

which accesses the same code, but passing a domain parameter in order to show a different microsite. The httpd.conf code for that looks like this:

 RewriteCond %{HTTP_HOST} ^([^./]+)\.example\.com$
 RewriteRule forums.html$ /browse.php?type=forums&domain=%1 [QSA]

Now I need to redirect http://example.com to http://www.example.com

I tried this, but it did not work:

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

(source: http://www.cyberciti.biz/faq/apache-redirect-domaincom-to-wwwdomaincom/ )

EDIT1

<VirtualHost IPADDRESS:80>
    ServerAlias *.example.com
    DocumentRoot /var/www/html/abc
    ServerName www.example.com
    UseCanonicalName On

EDIT2

Hi mreithub,

The setup I need is something like this:

http://X1.example.com should use the code in /something/X1

http://X2.example.com should use the code in /something/X2

http://example.com should redirect to http://www.example.com

http://www.example.com/scriptA.php should use the code in /var/www/html/abc/scriptA.php

http://whateverelse.example.com/scriptA.php should use the code in /var/www/html/abc/scriptA.php but be passed with a 'domain=whateverelse' parameter (but the URL on screen should show always show the domain as being http://whateverelse.example.com )

I had asked a question on SF - https://serverfault.com/questions/408805/configuring-httpd-conf-to-handle-wildcard-domains-with-multiple-scripts - from where I used adaptr's technique to pass the domain parameter to the PHP scripts.


回答1:


My personal favorite for redirecting whole VirtualHosts in apache is to simply create a VirtualHost for the domain to redirect and use the Redirect directive:

<VirtualHost IPADDRESS:80>
  ServerName example.com
  Redirect / http://www.example.com/
  DocumentRoot /var/www # <-- Just for completeness
</VirtualHost>

... and then another VirtualHost for your actual website

Redirect redirects every request going to host a to b while keeping any postfixes (e.g. http://example.com/foo?bar=bak becomes http://www.example.com/foo?bar=bak).

I use Redirect a lot to rewrite from http:// to https://




回答2:


Wow. 3 hours later... Lots of changes, lots of learnings.

1) Changed this:

   NameVirtualHost IPADDRESS:80

To:

   NameVirtualHost *:80

2) Marked all:

   <VirtualHost IPADDRESS:80>

As:

   <VirtualHost *:80>

3) Rearranged ServerName and placed it first within the VirtualHost (not sure if this made any difference)

<VirtualHost *:80>
    ServerName test4.example.com
    ServerAlias test4.example.com
    DocumentRoot /home/test4/public_html
    UseCanonicalName On
</VirtualHost>

3) Rearranged all VirtualHosts. Placed the 'static' / fixed subdomains earlier and the catch-all / www one as the last one. The last one looks like:

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com *.example.com
    DocumentRoot /var/www/html/abc
    UseCanonicalName On
    ...


来源:https://stackoverflow.com/questions/13280811/redirecting-http-example-com-to-http-www-example-com-while-keeping-http-sub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!