我正在尝试编写一个htaccess脚本,它自动添加www并从子文件夹中提取内容。
这是我用来从子目录中提取的脚本。
RewriteEngine On
RewriteBase /
# pointing for the domain abc.com to folder abc
ReWriteCond %{HTTP_HOST} abc.com
ReWriteCond %{REQUEST_URI} !abc/
ReWriteRule ^(.*)$ abc/$1 [L]
每当我尝试合并一些代码来自动添加www时,虽然它会以这种或那种方式搞砸。
答案 0 :(得分:2)
这样的事情应该有效。
<IfModule mod_rewrite.c>
#Turn on rewriting
RewriteEngine on
# Rewrite the non www. version
RewriteCond %{HTTP_HOST} !^www.abc.com$
RewriteRule ^(.*)$ http://www.abc.com/abc [L]
# Rewrite the www. version
RewriteCond %{HTTP_HOST} ^www.abc.com$
RewriteRule ^(.*)$ http://www.abc.com/abc [L]
</IfModule>
答案 1 :(得分:0)
这样的事情应该有效:
RewriteEngine On
RewriteBase /
# Rewrite to include www
RewriteCond %{HTTP_HOST} ^abc\.com [NC]
RewriteRule ^(.*)$ http://www.abc.com/ [L,R=301]
# Rewrite for the domain abc.com to folder abc
ReWriteCond %{HTTP_HOST} ^www\.abc\.com [NC]
ReWriteCond %{REQUEST_URI} !abc/ [NC]
ReWriteRule ^(.*)$ abc/$1 [L]
我想逃避任何实例。在一个URL中,因为在正则表达式中一个点将匹配任何东西,通常我自由地添加无案例[NC]标志。第一条规则应重定向以包含www,第二条规则应将所有其他路径引导至abc路径。