删除路径&通过htaccess

时间:2015-04-29 14:29:13

标签: apache .htaccess redirect

我知道这应该很容易,但它打败了我。站点移动后,我希望能够删除文件的路径以及扩展名(如果有)和查询字符串(如果有)和index.php(如果有),只留下文件名。 EG:

http://www.example.com/folder1/folder2/helloworld.html
301 to >> http://www.example.com/helloworld

http://example.com/anotherpath/apage.html
301 to >> http://www.example.com/apage

http://example.com/index.php/blurb/noextension
301 to >> http://www.example.com/noextension

http://www.example.com/longpath/withsome/morefolders/more.html?dkdhghjghjk
301 to >> http://www.example.com/more

原始网站中有许多不同的路径(有和没有www),但 如果 文件以.html结尾(可能有查询我也想把它重定向到域名/文件名

但是......为了使问题更复杂,他们无法重定向

http://www.example.com/category/something

这是因为该类别是新系统的一部分(它的wordpress)。

感谢您对此提供任何帮助!

============

更新 - 有一些问题,但几乎在那里!我调整了你建议的正则表达式,现在也删除.html和查询字符串(如果存在)我有这个。

RewriteRule ^.*/([^/]+)(.*)?\.h.*$ /$1? [L,NC,R=301]

但是,它会将主页重定向到

http://www.example.com
http://www.example.com/_index

令我困惑的是。我的正则表达式中有什么东西吗?

2 个答案:

答案 0 :(得分:1)

RewriteEngine On

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/category/
RewriteRule ^.*/([^/]+)\.html$ /$1? [L,NC,R]
RewriteCond %{REQUEST_URI} !^/category/
RewriteRule ^.*/([^/]+)$ /$1? [L,NC,R]

有两条规则,因为我不能单独写。 文件(.html)的第一个规则和目录的第二个规则(没有.html)。 标志 ? $ 1后删除查询

答案 1 :(得分:1)

写入.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 
相关问题