Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从url中删除www并同时使用子文件夹?_Php_Apache_.htaccess_Mod Rewrite_Url Rewriting - Fatal编程技术网

Php 如何从url中删除www并同时使用子文件夹?

Php 如何从url中删除www并同时使用子文件夹?,php,apache,.htaccess,mod-rewrite,url-rewriting,Php,Apache,.htaccess,Mod Rewrite,Url Rewriting,我对URL重写一无所知或知之甚少。我在HostGator上使用多域主机,遇到了一个问题。我想把每个网站在一个文件夹,包括主域 所以我想把我的网站像 public_html/site1 public_html/site2 public_html/site3 HostGator说我的主域必须指向public\u html文件夹,我不能直接指向site1文件夹。我需要在这里使用url重写。我不希望主域的URL中有www,我想从子目录运行网站 有人能给我完整的代码吗?我的主要问题是主域。我可以管理辅助

我对URL重写一无所知或知之甚少。我在HostGator上使用多域主机,遇到了一个问题。我想把每个网站在一个文件夹,包括主域

所以我想把我的网站像

public_html/site1
public_html/site2
public_html/site3
HostGator说我的主域必须指向
public\u html
文件夹,我不能直接指向
site1
文件夹。我需要在这里使用url重写。我不希望主域的URL中有
www
,我想从子目录运行网站


有人能给我完整的代码吗?我的主要问题是主域。我可以管理辅助域。

此代码适用于将主域(即
site1.com
重定向到子目录
/site1

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
RewriteCond %{REQUEST_URI} !^/site1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site1/$1
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
RewriteRule ^(/)?$ site1/index.php [L]
但是我仍然无法从我的主域url中删除
www.

试试这个

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

我没有对此进行测试,但它是您原始代码和Swaraj答案的组合:

RewriteEngine on

# Remove www. from primary domain
# per Swaraj's answer
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=302,QSA,NC,L]

# Per your code, rewrite to site1 directory
RewriteCond %{HTTP_HOST} ^site1.com$
RewriteCond %{REQUEST_URI} !^/site1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site1/$1

# Per your code, show primary domain root
RewriteCond %{HTTP_HOST} ^site1.com$
RewriteRule ^(/)?$ site1/index.php [L]

@迈克·安东尼谢谢你纠正了这个问题。现在看起来好多了。我知道你问了很多类似的问题。如果可能的话,请给能回答这个问题的人贴上标签。谢谢你的帮助,但是它不起作用。任何建议。我在上面提供的代码下面使用了它。只需使用这个,如果我只使用你提供的代码,它就会删除www,但它不会指向子目录。我想我们需要为此添加代码。我不知道.htaccess代码。请将此作为编辑添加到您的问题中,然后删除此答案。