简体   繁体   中英

subdomain wildcard .htaccess for escape cloudflare

I have example.com domain and set on cloudflare. But some time I don't need to use cloudflare, so I create up.example.com subdomain and unused of cloudflare for it. then I park it on main domain. That means the example.com and up.example.com point to root site. Now for cover duplicate content in SEO, I need to a .htaccess file.

These items is important for me:

  • All URLs under main domain (example.com) must be open and accessible.
  • The main page don't open with subdomain (up.example.com) and just open with main domain (example.com).
  • All address with upload controller must be accessible from subdomain. for example: up.example.com/upload/
  • The word after controller (my action) just is contain az, 0-9 (because don't allow use dot for point to a file)
  • About another URLs path must don't accessible from subdomain and must get 404 error.

For this, I write a code here:

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]

But in this code my problem is:

  • up.example.com => open it! but don't must open it.
  • up.example.com/upload/test => get me Not Found. The requested URL /index.php was not found on this server. Not Found. The requested URL /index.php was not found on this server. , but must open it.

Note: about another URLs I get correct 404 error, for example for up.example.com/upload/test.jpg I get Not Found. The requested URL /upload/test.jpg was not found on this server. Not Found. The requested URL /upload/test.jpg was not found on this server. error message, in this error message pointed exactly to /upload/test.jpg but about up.example.com/upload/test in error message pointed to index.php file!

Please assist me to have correction .htaccess code.

I resolve this problem with help in this link .
Just must use !^index\\.php$ instead .

correction code:

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM