Mod重写重定向直接访问

时间:2015-11-30 09:10:42

标签: regex apache .htaccess mod-rewrite redirect

  1. 我需要重定向调用index.html文件的所有用户。
  2. 我需要使用短链接http://sites.com/111,但脚本从fffolder / 111.php获取文件
  3. 我需要将希望直接访问http://sites.com/fffolder/111.php的用户重定向到index.html
  4. Redirect 301 /index.html http://sites.com/ 
    RewriteEngine On 
    RewriteRule ^([a-zA-Z0-9_-]+)$ fffolder/$1.php 
    RewriteRule ^([a-zA-Z0-9_-]+)/$ fffolder/$1.php  
    RewriteCond %{REQUEST_FILENAME} !-f  
    RewriteCond %{REQUEST_FILENAME} !-d
    #If file not foun redirect to index 
    RewriteRule .* index.html [PT]
    
    #Redirect direct access to all php files in fffolder 
    RewriteRule ^(.*)\.php$ index.html [R=301,L]
    

    1和2正在工作但3不工作,如何写出正确的语法?

1 个答案:

答案 0 :(得分:1)

您可以使用:

RewriteEngine On 

RewriteCond %{THE_REQUEST} /index\.html [NC]
RewriteRule ^(.*?)index\.html$ /$1 [L,R=301,NC]

#Redirect direct access to all php files in fffolder 
RewriteCond %{THE_REQUEST} /.+\.php [NC]
RewriteRule \.php$ /index.html [R=301,L,NC]

RewriteRule ^([\w-]+)/?$ fffolder/$1.php [L]

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d
#If file not foun redirect to index 
RewriteRule ^ index.html [L]