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

时间:2013-01-18 07:48:02

标签: .htaccess

我正在尝试重定向以下网址...

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

www.example.com/change/file 什么是.htaccess文件,因为我对此不太了解。

1 个答案:

答案 0 :(得分:2)

也许这会奏效:

# 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] mean是最后一条规则,它是永久重定向。如果您希望它是临时的,请更改为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