.htaccess RewriteRule ReDirect让我疯狂(IUpdate 02)

时间:2012-06-22 10:53:19

标签: .htaccess mod-rewrite

更新02

大家好,

 After much searching I found a very good clue that helped me take some important steps towards my goal. Everything is now working well except for the following:

I need to ReWrite or ReDirect to www.domain.com/go404 in these cases:
   www.domain.com
   www.domain.com/
   domain.com
   domain.com/

 But not, of course, when they type
    www.domain.com/subdomain or
    domain.com/subdomain

 Presently, my htaccess code has this snippet added:

    # This sends "just domain" visits to 404
    RewriteCond %{HTTP_HOST} ^domain.com$ [NC] 
    RewriteRule ^(.*)$ http://www.domain.com/go404 [R=301,L]

    # This one makes a NON WWW become a WWW
    RewriteCond %{HTTP_HOST} ^domain.com$ [NC] 
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

    # This strips the trailing slash
    RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]

Can anyone tell me how to set up the condition so it ONLY allows domains written without the trailing slash and/or subfolder string? As indicated above those should redirect to 404.

谢谢!


从我原来的问题更新:

大家好,我尝试了提供的解决方案,但它不起作用。回顾一下:

I'm trying to get our Magento site to respond as in the table below using .htaccess rewrites. For whatever reason, only some options work (marked as "Ok"). The others produce www.domain.com, that I don't want (marked as "Fail"). 

访问者类型时(内部)结果应为: --------------------------- ----------------------- --------------     www.domain.com www.domain.com/go404(失败)
    www.domain.com/ www.domain.com/go404(失败)
    www.domain.com/notfound www.domain.com/go404(OK)
    www.domain.com/subdomain www.domain.com/_subdomain(Ok)
    www.domain.com/subdomain/ www.domain.com/_subdomain(好)
    www.domain.com/admin www.domain.com/admin(OK)

domain.com          www.domain.com/go404        (Fail)   
domain.com/         www.domain.com/go404        (Fail)   
domain.com/notfound     www.domain.com/go404        (Fail)   
domain.com/subdomain    www.domain.com/_subdomain   (Fail)   
domain.com/subdomain/   www.domain.com/_subdomain   (Ok)   
domain.com/admin        www.domain.com/admin        (Fail)   

To keep things in order in our Root, all subdomains are folders called _subdomain. Visitors to the site only have to write www.domain.com/subdomain and we add the underscore in front.

注意: “notfound”是没有相应“_subdomain”文件夹的任何字符串 “admin”是一个有效的例外:前面没有下划线 从上表中可以看出,没有“www”的所有地址都失败了。另外,我有两个与“清理”域+干净域相关的特定问题,带有斜杠。

这是我完整的htaccess(我删除了评论以缩短评论):


addhandler x-httpd-php5-cgi .php5    
addhandler x-httpd-php5-cgi .php   
addhandler x-httpd-php-cgi .php4   

Options -MultiViews   
DirectoryIndex index.php  

<IfModule mod_php5.c>  
php_value memory_limit 256M  
php_value max_execution_time 18000  
php_flag magic_quotes_gpc off   
php_flag session.auto_start off
php_flag suhosin.session.cryptua off
php_flag zend.ze1_compatibility_mode Off
</IfModule>

<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>

<IfModule mod_deflate.c>

</IfModule>

<IfModule mod_ssl.c>
SSLOptions StdEnvVars
</IfModule>

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^api/([a-z][0-9a-z_]+)/?$ api.php?type=$1 [QSA,L]
RewriteRule ^api/rest api.php?type=rest [QSA,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>

AddDefaultCharset Off

<IfModule mod_expires.c>
ExpiresDefault "access plus 1 year"
</IfModule>

Order allow,deny
Allow from all

<Files RELEASE_NOTES.txt>
order allow,deny
deny from all
</Files>

RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^domain\.com/(admin)$ www.domain.com/$1 [L]
RewriteRule ^/(.*)$ www.domain.com/_$1  [L]
RewriteRule ^www\.domain\.com/(admin)$ www.domain.com/$1 [L]
RewriteRule ^www\.domain\.com/(.*)$ www.domain.com/_$1

感谢所有

老问题:

我正在尝试使用.htaccess重写让我们的网站以这种方式做出回应。无论出于何种原因,只有其中一个选项有效(标记为“Ok”),所有其他选项都生成www.domain.com(标记为“失败”)。

我需要实现这种组合:

When visitor types               The (internal) result should be:
-----------------------------    --------------------------------
www.domain.com                   www.domain.com/go404       (Fail)
www.domain.com/                  www.domain.com/go404       (Fail)
www.domain.com/subdomain         www.domain.com/_subdomain  (Ok)
www.domain.com/admin             www.domain.com/admin       (Ok)

domain.com                       www.domain.com/go404       (Fail)
domain.com/                      www.domain.com/go404       (Fail)
domain.com/subdomain             www.domain.com/_subdomain  (Fail)
domain.com/admin                 www.domain.com/admin       (Fail)

我的(root).htaccess文件包含:

RewriteRule ^/(admin) www.domain.com/$1
RewriteRule ^/(.*) www.domain.com/_$1  [L]

我尝试使用

强制domain.com进入www.doman.com
RewriteRule ^domain.com$ www.domain.com     (Fail)

顺便说一句:子域文件夹(_subdomain)也有.htaccess文件。我还需要在那里添加RewriteRule吗?我尝试过/没有用过。

1 个答案:

答案 0 :(得分:0)

我不确定你是如何使用这些重写的第3个案例。他们不应该工作。

以下是我要使用的内容:

#external redirect to www. version (to prevent duplicated content)
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule .* http://www.domain.com/$0 [R=302,L]

RewriteRule ^/?$ /go404
RewriteRule ^/?(admin) - [L]
RewriteRule ^/?(.*) /_$1  [L]