仅当文件夹存在时,网址才会重定向到文件夹

时间:2015-07-25 13:41:05

标签: regex apache .htaccess mod-rewrite url-rewriting

我想使用Url重写来转换它:

http://test.com/mypage

我已经有了ErrorDocument 404的一些代码,www。删除和删除.php和.html扩展名

ErrorDocument 404 http://test.net/404

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST}  ^www.test.net [NC]
RewriteRule ^(.*) http://test.net/$1 [L,R=301]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.*?)/?$ $1.html [L]

Options -Indexes

1 个答案:

答案 0 :(得分:2)

您可以使用:

ErrorDocument 404 /404.php

Options +FollowSymLinks -Indexes
RewriteEngine on

RewriteCond %{HTTP_HOST}  ^www\.(test\.net)$ [NC]
RewriteRule ^(.*) http://%1/$1 [L,R=301]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

RewriteCond %{DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.+?)/?$ pages/$1.php [L]