301将* .php重定向到非 - * .php

时间:2013-08-26 16:16:52

标签: regex apache .htaccess redirect mod-rewrite

我正在尝试将http://www.domain.tld/folder/filename.php 301的请求重定向到http://www.domain.tld/folder/filename(即没有php),并且遇到了一些困难。

这是我的.htaccess:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule (.*) $1.php [L]

RewriteRule (.*)\.php $1 [L,R=301]

First RewriteRule按照我的预期工作。第二......不是那么多。

http://www.domain.tld/folder/filename.php提出301重定向到http://www.domain.tld/home/username/public_html/folder/filename的书面请求

如果我将最后一个RewriteRule改为/$1而不是$1,我会被定向到http://www.domain.tld/filename而不是http://www.domain.tld/folder/filename

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

保留你的.htaccess,以隐藏.php扩展名

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]