【问题标题】:Url redirect to folder only if it exist网址仅在存在时才重定向到文件夹
【发布时间】:2015-07-25 23:06:37
【问题描述】:

我想使用 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

【问题讨论】:

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


    【解决方案1】:

    你可以使用:

    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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-08
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      相关资源
      最近更新 更多