对于www和非www,Htaccess重定向发生了不同

时间:2015-05-27 11:30:34

标签: .htaccess url-redirection

我已使用以下

将所有非www重定向到www url
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

接下来, 所需类别的最终网址是:

http://www.slidecorner.com/category/Design  

我用它来实现这个

RewriteRule ^/?category/(.*) category.php?ctitle=$1

这也有效,但是当我在任何重定向检查网站中检查此网址时,网址的“www”版本是完美的,但“非www”网址内部重定向到:

http://www.slidecorner.com/category.php/Design?ctitle=Design

这对搜索引擎有用吗?请指教

更新:也尝试了[L]标志,并且重写库已经存在。附上完整的htaccess供参考

<IfModule mod_rewrite.c>
RewriteEngine On 
RewriteBase /

RewriteRule ^docs$ docs.php
RewriteRule ^category$ category.php
RewriteRule ^categories$ categories.php
RewriteRule ^members$ members.php
RewriteRule ^upload$ upload.php
RewriteRule ^search$ search.php
RewriteRule ^account$ account.php
RewriteRule ^logout$ logout.php
RewriteRule ^login$ login.php
RewriteRule ^signup$ signup.php
RewriteRule ^feeds$ feeds.php
RewriteRule ^mydocs$ mydocs.php
RewriteRule ^myfav$ myfavoritedocs.php

RewriteRule ^doc/(.*)/(.*) viewdoc.php?did=$1&title=$2
RewriteRule ^syndicate/docs/(.*)/(.*) syndicatedocs.php?filter=$1&title=$2
RewriteRule ^download/doc/(.*)/(.*) downloaddoc.php?DID=$1&title=$2
RewriteRule ^resetpassword/(.*) resetpassword.php?code=$1
RewriteRule ^confirmemail/(.*) confirmemail.php?code=$1
RewriteRule ^resendconfirmation/(.*) resendconfirmation.php?userid=$1
RewriteRule ^members/profile/(.*)/(.*) memberprofile.php?pid=$1&username=$2

RewriteRule ^/?category/(.*) category.php?ctitle=$1 [L]



# to hide jpg images
RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?slidecorner.com[NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?slidecorner.*$ [NC] 
RewriteRule \.(jpeg|jpg)$ - [F]
# to hide jpg images


# for sending compressed data to load faster
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
# for sending compressed data to load faster


</IfModule>



# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymLinks
Options +SymLinksIfOwnerMatch
#non www redirect ,since the above doesnt redirect 

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


## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##



<IfModule mod_security.c> 
   # Turn off mod_security filtering. 
   SecFilterEngine Off 

   # The below probably isn't needed, 
   # but better safe than sorry. 
   SecFilterScanPOST Off 
</IfModule>


Options -Indexes

更新:我发现问题是由于此代码

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

我删除了,网址很好。非www到www重定向也完全停止,因为我删除了

上述代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

指令的顺序(模块内)在.htaccess中很重要。虽然您的问题表明您首先执行规范www重定向,但实际上在<。em>内部重写后,您的.htaccess文件中存在此,这在这种情况下肯定是个问题。 (虽然您从&#34;检查工具/网站&#34;获得的确切输出似乎是&#34;工具&#34; IMO的错误。)

通常,在内部重写之前,您应始终具有外部重定向

首先进行内部重写,最后丢失你的&#34;漂亮的网址&#34;。访问非规范(非www)网址http://example.com/category/Design时实际发生的情况是:

  1. 内部重写为:
    /category.php?ctitle=Design

  2. 外部重定向到:
    http://www.example.com/category.php?ctitle=Design

  3. 重要的是要意识到Llast)标志仅停止当前规则集的处理。如果发生内部或外部重定向,则再次开始处理!