子域通配符.htaccess for escape cloudflare

时间:2014-10-15 13:27:39

标签: apache .htaccess subdomain wildcard cloudflare

我有example.com域并设置为cloudflare。 但是有些时候我不需要使用cloudflare,所以我创建了up.example.com子域并且没有使用cloudflare。然后我把它停在主域。 这意味着example.comup.example.com指向根网站。 现在,为了覆盖SEO中的重复内容,我需要一个.htaccess文件。

这些项目对我很重要:

  • 主域(example.com)下的所有网址都必须是开放且可访问的。
  • 主页面未打开子域名(up.example.com),只打开主域名(example.com)。
  • 必须可以从子域访问具有upload控制器的所有地址。例如:up.example.com/upload /
  • 控制器之后的单词(我的动作)只包含a-z,0-9(因为不允许使用点来指向文件)
  • 关于其他网址路径必须无法从子域访问,并且必须出现404错误。

为此,我在这里写一个代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^up\..+ [NC]
RewriteCond %{REQUEST_URI} !^(/upload/?[a-z,A-Z,0-9]*)$ [NC]
RewriteRule . - [R=404,L,NS]

# Hide index.php , this used for YII PHP Framework
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]

但是在这段代码中我的问题是:

  • up.example.com =>打开它!但不要打开它。
  • up.example.com/upload/test =>得到我Not Found. The requested URL /index.php was not found on this server.,但必须打开它。

注意:关于其他网址,我得到了正确的404错误,例如up.example.com/upload/test.jpg我收到Not Found. The requested URL /upload/test.jpg was not found on this server.错误消息,此错误消息指向/upload/test.jpg但是大约up.example.com/upload/test错误消息指向index.php文件!

请帮我修正.htaccess代码。

1 个答案:

答案 0 :(得分:0)

我在this link的帮助下解决了这个问题 只需使用!^index\.php$代替.

更正码:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^up\. [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php[?\s] [NC]
RewriteRule ^index\.php$ - [R=404,L,NC]

RewriteCond %{HTTP_HOST} ^up\. [NC]
RewriteCond %{REQUEST_URI} !^(/upload/?[a-z,A-Z,0-9]*)$ [NC]
RewriteRule !^index\.php$ - [R=404,L,NC]

# Hide index.php , this used for YII PHP Framework
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]