htaccess没有使用重写规则正确重定向

时间:2015-07-17 05:53:13

标签: .htaccess mod-rewrite

我试图创建一个简单的htaccess文件来执行以下操作:

  1. 将所有http://domain.com重定向到http://www.domain.com
  2. 允许' .php'要从文件名中删除的扩展名
  3. 允许将http://www.domain.com/page/varhttp://www.domain.com/page/var/视为http://www.domain.com/page?u=var
  4. 将所有http://www.domain.com/page?u=var重写为http://www.domain.com/page/var
  5. 这是我到目前为止所做的,但它似乎并没有起作用:

        IndexIgnore .htaccess
        DirectoryIndex index.php
    
        Options -Indexes
    
        RewriteEngine On
    
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
    
        RewriteCond %{HTTP_HOST} !^www\.
        RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
        RewriteRule ^([^/.]+)/?$ $1.php
    
        RewriteRule page1/(.*)/$ /page1?u=$1
        RewriteRule page1/(.*)$ /page1?u=$1
        RewriteRule page2/(.*)/$ /page2?t=$1
        RewriteRule page2/(.*)$ /page2?t=$1
    
        ErrorDocument 400 http://www.domain.com/error?e=400
        ErrorDocument 401 http://www.domain.com/error?e=401
        ErrorDocument 403 http://www.domain.com/error?e=403
        ErrorDocument 404 http://www.domain.com/error?e=404
        ErrorDocument 500 http://www.domain.com/error?e=500
    
        AddOutputFilterByType DEFLATE text/plain
        AddOutputFilterByType DEFLATE text/html
        AddOutputFilterByType DEFLATE text/xml
        AddOutputFilterByType DEFLATE text/css
        AddOutputFilterByType DEFLATE application/xml
        AddOutputFilterByType DEFLATE application/xhtml+xml
        AddOutputFilterByType DEFLATE application/rss+xml
        AddOutputFilterByType DEFLATE application/javascript
        AddOutputFilterByType DEFLATE application/x-javascript
    

    任何帮助都将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:1)

试试这个:

    IndexIgnore .htaccess
    DirectoryIndex index.php

    Options -Indexes

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

 #Remove .php 
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME}\.php -f
 RewriteRule ^([^.]+)/?$ /$1.php [L,NC]

  RewriteCond %{THE_REQUEST} /page\?u=([^\s]+)
    RewriteRule ^ /page/%1? [NE,R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /page?u=$2 [L,NC]