简体   繁体   中英

htaccess rule to redirect from subdomains but keep some files accessible

<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
</Files>
AddType application/x-httpd-php .htm .xml .rss
Options Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !index\.php
RewriteCond %{HTTP_HOST} ^(.+?)\.mysite\.net$
RewriteRule .* /index.php?sitename=%1 [L]

Options +FollowSymLinks

Thas how my htaccess files looks like. I want all subdomain.mysite.com to redirect to my index file with the name of the subdomain so i can show the appropriate site. Thats working fine, so far...

My problem is that i now need to access for example www.mysite.com/script.php directly and i am getting a 500 error. htaccess is very confusing to me, how would i achieve this? Thanks!

You need to add these 3 lines to your existing rules:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

So the final rules would look like this:

<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
</Files>
AddType application/x-httpd-php .htm .xml .rss
Options Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !index\.php
RewriteCond %{HTTP_HOST} ^(.+?)\.mysite\.net$
RewriteRule .* /index.php?sitename=%1 [L]

Options +FollowSymLinks

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