如何使用.htaccess重定向到certian路径

如何使用.htaccess重定向到certian路径,.htaccess,.htaccess,我正在尝试重定向以下url www.example.com/test/test2/test3/file.html 至www.example.com/change/file 什么是.htaccess文件,因为我对此知之甚少。也许这样可以: # Start the rewrite engine RewriteEngine On # Assume the base is root and there are no alias RewriteBase / # Prevent loops if the

我正在尝试重定向以下url

www.example.com/test/test2/test3/file.html 
www.example.com/change/file
什么是.htaccess文件,因为我对此知之甚少。

也许这样可以:

# Start the rewrite engine
RewriteEngine On
# Assume the base is root and there are no alias
RewriteBase /
# Prevent loops if the directory exists
RewriteCond %{REQUEST_FILENAME} !-d
# Make sure there is an html file in the URL and capture it's filename if so.    
RewriteCond %{REQUEST_URI} ([^/]+)\.html/?  [NC]
# Use only the filename as target directory.
RewriteRule .*  %1  [L,R=301]
标志[L,R=301]表示最后一条规则,它是一个永久重定向。如果希望它是暂时的,请更改为302;如果希望静默映射,请删除整个标志
R=301

已更新

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ([^/]+)\.html/?  [NC]
RewriteRule .*  change/%1  [R=301,L]

# Second rule set
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ([^/]+)/?  [NC]
RewriteRule .*  %1/%1.html  [L]
永久重定向此文件

http://www.example.com/test/test2/test3/file.html

http://www.example.com/change/file
,带有到的静默映射


http://www.example.com/change/file/file.html

你能再多解释一点吗?我确实不太明白。当然。我会在回答中这样做。我尝试了你的建议,结果是,当它点击www.example.com/test/test2/test3/file.html时,它会重定向到www.example.com/file…我在问问题时犯了一个小错误,我对问题做了一点修改…请检查问题。另外,www.example.com/file给出了404,但是我想在下面的url www.example.com/change/fileI中显示file.html的输出我看到了您的更新。它是一个固定的名称还是一个变量?它是一个固定的名称,你能说这个regexp不会影响任何地方吗