.htaccess忽略某些目录但传递子域

时间:2014-12-18 04:31:44

标签: apache .htaccess mod-rewrite url-rewriting

现有.htaccess文件

RewriteEngine on
RewriteBase /

# if not https, redirect
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# parse the subdomain as a variable we can access
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^app
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^(.*)$ /$1?sub=%1

# ignore any .cfm|.html|.asp|.ico files and certain directories
RewriteRule ^(php|images|css|js|xyz|(.*)\.php|(.*)\.html|(.*)\.asp|(.*)\.ico)($|/) - [L]

# map all requests to the 'path' get variable in index.cfm
RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA]

=======

currently its doing this:

https://hello.domain.com --> https://hello.domain.com/index.php?sub=hello
https://hello.domain.com/dir1/15 --> https://hello.domain.com/index.php?path=dir1/15&sub=hello
https://hello.domain.com/index.php --> https://hello.domain.com/index.php
https://hello.domain.com/images/logo.png --> https://hello.domain.com/images/logo.png

what needs to happen:

1. https://hello.domain.com/index.php --> https://hello.domain.com/index.php?sub=hello

(目前没有追加sub = hello)

2. https://hello.domain.com/process/generate.php --> https://hello.domain.com/process/generate.php?sub=hello

(" / process"目录需要忽略,但" sub = hello"仍然需要传递)

基本上我需要对规则进行一些补充以确保#1& #2也在工作。不应对现有规则进行任何更改。

提前致谢!

1 个答案:

答案 0 :(得分:0)

你的.htaccess:

RewriteEngine on
RewriteBase /

# if not https, redirect
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# ignore any .cfm|.html|.asp|.ico files and certain directories
RewriteRule ^(php|images|css|js|xyz|(.*)\.html|(.*)\.asp|(.*)\.ico)($|/) - [L]

# parse the subdomain as a variable we can access
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^app
RewriteCond %{HTTP_HOST} ^([^.]+)\.[^.]+\.[^.]+$
RewriteRule ^(.*)$ index.php?sub=%1&path=$1 [L,QSA]

%{ENV:REDIRECT_STATUS}是一个内部变量,在第一次内部重写后设置为200。