如何设置Apache mod_rewrite重定向规则?

时间:2014-10-01 11:44:29

标签: php apache .htaccess mod-rewrite drupal-7

我对Apache设置不太熟悉。我需要让网站加载子目录内容,除了一页。

目前有一个网站,需要打电话给http://www.domain.com& http://domain.com加载http://www.domain.com/subfolder内容(但看起来像http://www.domain.com

http://www.domain.com/checkout页面外,此页面应重定向到https://www.domain.com/checkout以进行安全结帐

当前的mod_rewrite如下所示:

RewriteEngine on

RewriteRule ^$ domain/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/domain%{REQUEST_URI} -f
RewriteRule .* domain/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* domain/index.php?q=$0 [QSA]

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://domain.com.au/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://domain.com.au$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain.com.au/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain.com.au$      [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://www.domain.com.au [R,NC]

1 个答案:

答案 0 :(得分:1)

在网络服务器的根目录中打开名为.htaccess的文件,并添加以下代码行:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

RewriteCond %{SERVER_PORT} 80
RewriteRule ^checkout/(.*)$ https://www.yourdomain.com/checkout/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_URI} !^/checkout/
RewriteRule ^(.*)$ /subfolder/$1 [NE,L,QSA]

重写完整的.htaccess文件(检查是否有效,然后我将删除以前的代码):

RewriteRule ^$ subfolder/index.php [QSA,L]

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://www.yourdomain.com [NC]

RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

RewriteCond %{SERVER_PORT} 80
RewriteRule ^checkout/(.*)$ https://www.yourdomain.com/checkout/$1 [R=301,L]

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

RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_URI} !^/checkout/
RewriteRule ^(.*)$ /subfolder/$1 [NE,L,QSA]