在.htaccess中重写对重定向子域中的index.php的调用

时间:2013-12-19 21:37:00

标签: php apache .htaccess mod-rewrite joomla

我的服务器中当前有两个域,一个用于主域(www.domain.com),另一个用于测试域(test.domain.com)。每个域都指向一个包含Joomla安装的文件夹,每个文件夹中都有安装附带的标准.htaccess文件。

主域工作正常,但测试域不能按预期工作。如果我浏览到test.domain.com我可以正确看到主页,但如果我在其他任何地方浏览(例如test.domain.com/products.html),我会收到500错误。但是,如果我在子域之后追加index.php,它会按预期工作(即test.domain.com/index.php/products.html)。

这是.htaccess文件夹中的/public_html文件:

Options +FollowSymLinks

RewriteEngine On
RewriteBase /

# pointing www.domain.com to live
RewriteCond %{HTTP_HOST} !test.domain.com
ReWriteCond %{HTTP_HOST} www.domain.com
ReWriteCond %{REQUEST_URI} !^/live/
ReWriteRule ^(.*)$ live/$1 [L]

# pointing test.domain.com to test
ReWriteCond %{HTTP_HOST} test.domain.com
ReWriteCond %{REQUEST_URI} !^/test/
ReWriteRule ^(.*)$ test/$1 [L]

# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.)
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ([^/]+)$ $1/ [L]

# Rewrite any calls to /* to the index.php file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php/$1 [L]

以下是服务器中的文件夹结构:

/public_html
    |
    |--/live             <-- www.domain.com
    |     |
    |     |--.htaccess   <-- joomla standard .htaccess
    |
    |--/test             <-- test.domain.com
    |     |
    |     |--.htaccess   <-- joomla standard .htaccess
    |
    |--.htaccess

.htaccesslive文件夹中的test个文件分别包含RewriteBase /liveRewriteBase /test

我在stackoverflow和许多其他网站中发现了几个关于子域重定向的问题,但没有一个解决这个特定问题。有谁知道我错过了什么?

感谢。

3 个答案:

答案 0 :(得分:3)

您是否尝试在主域之前放置子域规则? 这是我的.htaccess,适用于真实项目。

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^test\.domain.com [NC]
RewriteRule ^.*$ index-test.php [NC,QSA] 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php

答案 1 :(得分:0)

我设法解决了这个问题,部分是@witzawitz关于重新排列子域规则的评论,还删除了RewriteBase /test文件夹中.htaccess文件中的/test行和RewriteBase /live .htaccess文件夹中/live文件中的行。

这是解决方案:

.htaccess文件夹中的

/public_html文件:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# pointing test.domain.com to test
ReWriteCond %{HTTP_HOST} test.domain.com
ReWriteCond %{REQUEST_URI} !^/test/
ReWriteRule ^(.*)$ test/$1 [L]

# pointing www.domain.com to live
RewriteCond %{HTTP_HOST} !test.domain.com
ReWriteCond %{HTTP_HOST} www.domain.com
ReWriteCond %{REQUEST_URI} !^/live/
ReWriteRule ^(.*)$ live/$1 [L]
.htaccess/public_html/live个文件夹中的

/public_html/test个文件:

## I have removed license and unnecessary comments 

# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the request is for something within the component folder,
# or for the site root, or for an extensionless URL, or the
# requested URL ends with one of the listed extensions
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]

答案 2 :(得分:0)

如果您要将所有流量重定向到* .example.com / path / to / request到* .example.com / index.php,其中*是子域和/或www,甚至省略,放在public_html中的代码下方/.htaccess:

Options + FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /

然后,您可以通过

访问path / to / request
$_SERVER['REQUEST_URI']